|
| 1 | +package tech.ydb.yoj.repository.ydb; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import tech.ydb.core.grpc.GrpcTransport; |
| 5 | +import tech.ydb.scheme.SchemeClient; |
| 6 | +import tech.ydb.table.SessionPoolStats; |
| 7 | +import tech.ydb.table.TableClient; |
| 8 | +import tech.ydb.topic.TopicClient; |
| 9 | +import tech.ydb.yoj.repository.ydb.client.SessionManager; |
| 10 | +import tech.ydb.yoj.repository.ydb.client.YdbSchemaOperations; |
| 11 | +import tech.ydb.yoj.repository.ydb.client.YdbSessionManager; |
| 12 | +import tech.ydb.yoj.util.lang.Exceptions; |
| 13 | + |
| 14 | +/*package*/ final class SessionClient implements AutoCloseable { |
| 15 | + private final TableClient tableClient; |
| 16 | + private final SchemeClient schemeClient; |
| 17 | + private final TopicClient topicClient; |
| 18 | + |
| 19 | + @Getter |
| 20 | + private final SessionManager sessionManager; |
| 21 | + |
| 22 | + @Getter |
| 23 | + private final YdbSchemaOperations schemaOperations; |
| 24 | + |
| 25 | + private final SessionMetrics metrics; |
| 26 | + |
| 27 | + /*package*/ SessionClient(YdbConfig config, YdbRepository.Settings repositorySettings, GrpcTransport transport) { |
| 28 | + this.tableClient = createClient(config, repositorySettings, transport); |
| 29 | + this.schemeClient = SchemeClient.newClient(transport).build(); |
| 30 | + this.topicClient = TopicClient.newClient(transport).build(); |
| 31 | + |
| 32 | + this.sessionManager = new YdbSessionManager(tableClient, config.getSessionCreationTimeout()); |
| 33 | + this.schemaOperations = new YdbSchemaOperations( |
| 34 | + config.getTablespace(), sessionManager, schemeClient, topicClient |
| 35 | + ); |
| 36 | + |
| 37 | + this.metrics = new SessionMetrics(tableClient, repositorySettings.metrics()); |
| 38 | + } |
| 39 | + |
| 40 | + public boolean isHealthy() { |
| 41 | + // We consider the database healthy if the number of sessions in the pool is greater than 0. |
| 42 | + // Bad sessions will be dropped either due to keep-alive or on the very first error that occurs in that session. |
| 43 | + // |
| 44 | + // If idleCount == 0, this may mean that the application has just started, or that the database cannot handle the load. |
| 45 | + // To account for that case, we check pendingAcquireCount (how many clients are waiting to acquire a session), |
| 46 | + // and if it’s more than maxSize of the client queue, we consider the database to be unhealthy. |
| 47 | + SessionPoolStats sessionPoolStats = tableClient.sessionPoolStats(); |
| 48 | + return sessionPoolStats.getIdleCount() > 0 || |
| 49 | + //todo: maybe we should consider pendingAcquireCount > 0 problematic, because there are clients waiting? |
| 50 | + sessionPoolStats.getPendingAcquireCount() <= sessionPoolStats.getMaxSize(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void close() { |
| 55 | + Exceptions.closeAll(metrics, tableClient, schemeClient, topicClient); |
| 56 | + } |
| 57 | + |
| 58 | + private static TableClient createClient( |
| 59 | + YdbConfig config, YdbRepository.Settings repositorySettings, GrpcTransport transport |
| 60 | + ) { |
| 61 | + return buildTableClient(repositorySettings, transport) |
| 62 | + .keepQueryText(false) |
| 63 | + .sessionKeepAliveTime(config.getSessionKeepAliveTime()) |
| 64 | + .sessionMaxIdleTime(config.getSessionMaxIdleTime()) |
| 65 | + .sessionPoolSize(config.getSessionPoolMin(), config.getSessionPoolMax()) |
| 66 | + .build(); |
| 67 | + } |
| 68 | + |
| 69 | + private static TableClient.Builder buildTableClient( |
| 70 | + YdbRepository.Settings repositorySettings, GrpcTransport transport |
| 71 | + ) { |
| 72 | + // TODO(nvamelichev@): Replace this with expression switch with type pattern as soon as we migrate to Java 21+ |
| 73 | + var queryImplementation = repositorySettings.queryImplementation(); |
| 74 | + if (queryImplementation instanceof QueryImplementation.TableService) { |
| 75 | + return TableClient.newClient(transport); |
| 76 | + } else if (queryImplementation instanceof QueryImplementation.QueryService) { |
| 77 | + return tech.ydb.query.impl.TableClientImpl.newClient(transport); |
| 78 | + } else { |
| 79 | + throw new UnsupportedOperationException("Unknown QueryImplementation: <" + queryImplementation.getClass() + ">"); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments