Search in sources :

Example 6 with ClientId

use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.

the class SessionExecutor method run.

public <T> T run(BaseMysqlOp<T> parallelTxnOp, String traceName) throws SessionExecutorPleaseRetryException {
    ClientId clientId = parallelTxnOp.getClientId();
    TxnClientManager clientManager = (TxnClientManager) datarouterClients.getClientManager(clientId);
    try {
        startTrace(traceName);
        clientManager.reserveConnection(clientId);
        return innerRun(clientId, clientManager, parallelTxnOp);
    } finally {
        finishTrace(traceName);
    }
}
Also used : TxnClientManager(io.datarouter.client.mysql.TxnClientManager) ClientId(io.datarouter.storage.client.ClientId)

Example 7 with ClientId

use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.

the class SqsQueuesDailyDigest method buildContent.

private Optional<ContainerTag<?>> buildContent(ContentType contentType) {
    ClientId clientId = Scanner.of(datarouterClients.getClientIds()).include(client -> datarouterClients.getClientTypeInstance(client) instanceof SqsClientType).findFirst().orElse(null);
    if (clientId == null) {
        return Optional.empty();
    }
    List<String> unreferencedQueues = queueRegistryService.getSqsQueuesForClient(clientId).getRight();
    if (unreferencedQueues.isEmpty()) {
        return Optional.empty();
    }
    TableTag unreferencedQueuesTable = new TableTag();
    if (contentType == ContentType.PAGE) {
        unreferencedQueuesTable = new J2HtmlTable<String>().withClasses("sortable table table-sm table-striped my-4 border").withHtmlColumn(th("Queue Name"), TagCreator::td).build(unreferencedQueues);
    } else if (contentType == ContentType.EMAIL) {
        unreferencedQueuesTable = new J2HtmlEmailTable<String>().withColumn("Queue Name", row -> row).build(unreferencedQueues);
    }
    ContainerTag<?> header = digestService.makeHeader("Unreferenced Sqs Queues", paths.datarouter.client.inspectClient, "?clientName=sqs");
    return Optional.of(div(header, unreferencedQueuesTable));
}
Also used : J2HtmlTable(io.datarouter.web.html.j2html.J2HtmlTable) Scanner(io.datarouter.scanner.Scanner) J2HtmlEmailTable(io.datarouter.email.html.J2HtmlEmailTable) DatarouterWebPaths(io.datarouter.web.config.DatarouterWebPaths) DailyDigestGrouping(io.datarouter.web.digest.DailyDigestGrouping) Singleton(javax.inject.Singleton) TableTag(j2html.tags.specialized.TableTag) ZoneId(java.time.ZoneId) ContainerTag(j2html.tags.ContainerTag) Inject(javax.inject.Inject) List(java.util.List) TagCreator.th(j2html.TagCreator.th) ClientId(io.datarouter.storage.client.ClientId) Optional(java.util.Optional) DailyDigest(io.datarouter.web.digest.DailyDigest) TagCreator.div(j2html.TagCreator.div) J2HtmlTable(io.datarouter.web.html.j2html.J2HtmlTable) TagCreator(j2html.TagCreator) SqsClientType(io.datarouter.aws.sqs.SqsClientType) DatarouterClients(io.datarouter.storage.client.DatarouterClients) DailyDigestService(io.datarouter.web.digest.DailyDigestService) SqsClientType(io.datarouter.aws.sqs.SqsClientType) TagCreator(j2html.TagCreator) TableTag(j2html.tags.specialized.TableTag) ClientId(io.datarouter.storage.client.ClientId) J2HtmlEmailTable(io.datarouter.email.html.J2HtmlEmailTable)

Example 8 with ClientId

use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.

the class SqsUpdateQueueHandler method purgeQueue.

@Handler
private Mav purgeQueue(@Param(PARAM_clientName) String clientName, @Param(PARAM_queueName) String queueName, @Param(PARAM_referer) String referer) {
    ClientId clientId = datarouterClients.getClientId(clientName);
    AmazonSQS sqs = sqsClientManager.getAmazonSqs(clientId);
    GetQueueUrlResult queueUrlResult = sqs.getQueueUrl(queueName);
    sqs.purgeQueue(new PurgeQueueRequest(queueUrlResult.getQueueUrl()));
    String message = "Purged SQS queue: " + queueName;
    var dto = new DatarouterChangelogDtoBuilder("Sqs", queueName, "purgeQueue", getSessionInfo().getRequiredSession().getUsername()).build();
    changelogRecorder.record(dto);
    return buildPage(referer, message);
}
Also used : PurgeQueueRequest(com.amazonaws.services.sqs.model.PurgeQueueRequest) ClientId(io.datarouter.storage.client.ClientId) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) GetQueueUrlResult(com.amazonaws.services.sqs.model.GetQueueUrlResult) DatarouterChangelogDtoBuilder(io.datarouter.instrumentation.changelog.ChangelogRecorder.DatarouterChangelogDtoBuilder) BaseHandler(io.datarouter.web.handler.BaseHandler)

Example 9 with ClientId

use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.

the class SpannerSingleTableSchemaUpdateService method performSchemaUpdate.

public Optional<SchemaUpdateResult> performSchemaUpdate(ClientId clientId, Supplier<List<String>> existingTableNames, PhysicalNode<?, ?, ?> physicalNode) {
    String tableName = physicalNode.getFieldInfo().getTableName();
    List<Field<?>> primaryKeyFields = physicalNode.getFieldInfo().getSamplePrimaryKey().getFields();
    List<? extends SpannerBaseFieldCodec<?, ?>> primaryKeyCodecs = fieldCodecRegistry.createCodecs(primaryKeyFields);
    for (SpannerBaseFieldCodec<?, ?> codec : primaryKeyCodecs) {
        if (codec.getSpannerColumnType().isArray()) {
            throw new RuntimeException("Invalid field type used for primary key: " + codec.getField().getKey().getName());
        }
    }
    List<SpannerIndex> indexes = new ArrayList<>();
    List<SpannerIndex> uniqueIndexes = Scanner.of(physicalNode.getFieldInfo().getUniqueIndexes().entrySet()).map(entry -> new SpannerIndex(tableName, entry.getKey(), entry.getValue(), Collections.emptyList(), true)).list();
    var statements = new SpannerUpdateStatements();
    String entityTableName = null;
    if (physicalNode instanceof IndexedStorage) {
        IndexedStorage<?, ?> indexedStorage = (IndexedStorage<?, ?>) physicalNode;
        indexes = Scanner.of(indexedStorage.getManagedNodes()).map(node -> new SpannerIndex(tableName, node.getName(), node.getIndexEntryFieldInfo().getPrimaryKeyFields(), node.getIndexEntryFieldInfo().getFields(), false)).list();
    }
    List<SpannerColumn> primaryKeyColumns = Scanner.of(primaryKeyCodecs).map(codec -> codec.getSpannerColumn(false)).list();
    List<SpannerColumn> nonKeyColumns = Scanner.of(fieldCodecRegistry.createCodecs(physicalNode.getFieldInfo().getNonKeyFields())).map(codec -> codec.getSpannerColumn(true)).list();
    if (!existingTableNames.get().contains(tableName)) {
        statements.updateFunction(tableOperationsGenerator.createTable(tableName, primaryKeyColumns, nonKeyColumns, entityTableName), updateOptions::getCreateTables, true);
        Scanner.of(indexes, uniqueIndexes).concat(Scanner::of).map(index -> createIndex(index, primaryKeyColumns)).forEach(statement -> statements.updateFunction(statement, updateOptions::getCreateTables, true));
    } else {
        DatabaseClient databaseClient = clientsHolder.getDatabaseClient(clientId);
        List<SpannerColumn> allColumns = Scanner.of(primaryKeyColumns, nonKeyColumns).concat(Scanner::of).list();
        ResultSet columnRs = databaseClient.singleUse().executeQuery(Statement.of(tableOperationsGenerator.getTableSchema(tableName)));
        ResultSet primaryKeyRs = databaseClient.singleUse().executeQuery(Statement.of(tableOperationsGenerator.getTableIndexColumnsSchema(tableName, "PRIMARY_KEY")));
        tableAlterSchemaService.generateUpdateStatementColumns(tableName, allColumns, primaryKeyColumns, columnRs, primaryKeyRs, statements);
        ResultSet indexesRs = databaseClient.singleUse().executeQuery(Statement.of(tableOperationsGenerator.getTableIndexSchema(tableName)));
        Set<String> currentIndexes = tableAlterSchemaService.getIndexes(indexesRs);
        Scanner.of(indexes, uniqueIndexes).concat(Scanner::of).forEach(index -> {
            Statement tableIndexColumnsSchema = Statement.of(tableOperationsGenerator.getTableIndexColumnsSchema(tableName, index.getIndexName()));
            ResultSet indexRs = databaseClient.singleUse().executeQuery(tableIndexColumnsSchema);
            if (!tableAlterSchemaService.indexEqual(index, indexRs)) {
                if (currentIndexes.contains(index.getIndexName())) {
                    statements.updateFunction(tableOperationsGenerator.dropIndex(index.getIndexName()), updateOptions::getDropIndexes, false);
                }
                statements.updateFunction(createIndex(index, primaryKeyColumns), updateOptions::getAddIndexes, true);
            }
            currentIndexes.remove(index.getIndexName());
        });
        currentIndexes.forEach(name -> statements.updateFunction(tableOperationsGenerator.dropIndex(name), updateOptions::getDropIndexes, false));
    }
    String errorMessage = null;
    if (!statements.getExecuteStatements().isEmpty()) {
        logger.info(SchemaUpdateTool.generateFullWidthMessage("Executing Spanner " + getClass().getSimpleName() + " SchemaUpdate"));
        logger.info(String.join("\n\n", statements.getExecuteStatements()));
        Database database = clientsHolder.getDatabase(clientId);
        OperationFuture<Void, UpdateDatabaseDdlMetadata> future = database.updateDdl(statements.getExecuteStatements(), null);
        errorMessage = FutureTool.get(future.getPollingFuture().getAttemptResult()).getErrorMessage();
        if (StringTool.notNullNorEmptyNorWhitespace(errorMessage)) {
            logger.error(errorMessage);
        }
    }
    if (statements.getPreventStartUp()) {
        errorMessage = "an alter on Spanner table " + tableName + " is required";
    }
    if (statements.getPrintStatements().isEmpty()) {
        return Optional.empty();
    }
    String printStatement = statements.getPrintStatements().stream().map(statement -> statement + ";").collect(Collectors.joining("\n"));
    SchemaUpdateTool.printSchemaUpdate(logger, printStatement);
    return Optional.of(new SchemaUpdateResult(printStatement, errorMessage, clientId));
}
Also used : Scanner(io.datarouter.scanner.Scanner) SchemaUpdateTool(io.datarouter.storage.config.schema.SchemaUpdateTool) OperationFuture(com.google.api.gax.longrunning.OperationFuture) SpannerDatabaseClientsHolder(io.datarouter.gcp.spanner.connection.SpannerDatabaseClientsHolder) LoggerFactory(org.slf4j.LoggerFactory) PhysicalNode(io.datarouter.storage.node.type.physical.PhysicalNode) DatabaseClient(com.google.cloud.spanner.DatabaseClient) Singleton(javax.inject.Singleton) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ResultSet(com.google.cloud.spanner.ResultSet) ClientId(io.datarouter.storage.client.ClientId) Logger(org.slf4j.Logger) Database(com.google.cloud.spanner.Database) Set(java.util.Set) StringTool(io.datarouter.util.string.StringTool) Collectors(java.util.stream.Collectors) SchemaUpdateOptions(io.datarouter.storage.config.schema.SchemaUpdateOptions) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Statement(com.google.cloud.spanner.Statement) List(java.util.List) Field(io.datarouter.model.field.Field) SpannerFieldCodecRegistry(io.datarouter.gcp.spanner.field.SpannerFieldCodecRegistry) Optional(java.util.Optional) IndexedStorage(io.datarouter.storage.node.op.raw.IndexedStorage) Collections(java.util.Collections) SpannerBaseFieldCodec(io.datarouter.gcp.spanner.field.SpannerBaseFieldCodec) SchemaUpdateResult(io.datarouter.storage.config.schema.SchemaUpdateResult) FutureTool(io.datarouter.util.concurrent.FutureTool) Scanner(io.datarouter.scanner.Scanner) ArrayList(java.util.ArrayList) Field(io.datarouter.model.field.Field) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) ResultSet(com.google.cloud.spanner.ResultSet) Database(com.google.cloud.spanner.Database) IndexedStorage(io.datarouter.storage.node.op.raw.IndexedStorage) Statement(com.google.cloud.spanner.Statement) DatabaseClient(com.google.cloud.spanner.DatabaseClient) SchemaUpdateResult(io.datarouter.storage.config.schema.SchemaUpdateResult)

Example 10 with ClientId

use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.

the class DatarouterHomepageHandler method buildClientsTable.

private String buildClientsTable() {
    boolean hasUninitializedClients = false;
    List<ClientsJspDto> clients = new ArrayList<>();
    for (ClientId clientId : datarouterClients.getClientIds()) {
        boolean initialized = clientInitializationTracker.isInitialized(clientId);
        hasUninitializedClients = hasUninitializedClients || !initialized;
        String clientTypeName = datarouterClients.getClientTypeInstance(clientId).getName();
        CheckResultJspDto checkResultJspDto = new CheckResultJspDto(monitoringService.getLastResultForDatarouterClient(clientId), monitoringService.getGraphLinkForDatarouterClient(clientId));
        clients.add(new ClientsJspDto(clientId.getName(), clientTypeName, initialized, checkResultJspDto));
    }
    String servletContextPath = servletContext.get().getContextPath();
    var tbody = tbody();
    clients.forEach(clientsJspDto -> {
        String clientName = clientsJspDto.clientName;
        ContainerTag<?> leftTd;
        ContainerTag<?> rightTd;
        if (clientsJspDto.initialized) {
            ContainerTag<?> latencyGraphTag;
            CheckResultJspDto checkResultJspDto = clientsJspDto.checkResult;
            if (checkResultJspDto == null || checkResultJspDto.getCheckResult() == null) {
                latencyGraphTag = span().withClass("status");
            } else {
                latencyGraphTag = a().withHref(checkResultJspDto.getGraphLink()).withClass("status " + checkResultJspDto.getCssClass());
            }
            String inspectLinkPath = servletContextPath + paths.datarouter.client.inspectClient.toSlashedString() + "?clientName=" + clientName;
            leftTd = td(TagCreator.join(latencyGraphTag, a(clientName).withHref(inspectLinkPath)));
            rightTd = td(clientsJspDto.clientTypeName);
        } else {
            leftTd = td(TagCreator.join(span().withClass("status"), clientName));
            String initLinkPath = servletContextPath + paths.datarouter.client.initClient.toSlashedString() + "?clientName=" + clientName;
            rightTd = td(TagCreator.join("[", a("init").withHref(initLinkPath), "]"));
        }
        tbody.with(tr(leftTd, rightTd));
    });
    var caption = caption("Clients");
    if (hasUninitializedClients) {
        String linkPath = servletContextPath + paths.datarouter.client.initAllClients.toSlashedString();
        caption = caption(TagCreator.join("Clients [", a("init remaining clients").withHref(linkPath), "]"));
    }
    var table = table(caption.withStyle("caption-side: top"), tbody).withClass("table table-striped table-bordered table-sm");
    return div(table).withClass("col-12 col-sm-6").render();
}
Also used : ArrayList(java.util.ArrayList) ClientId(io.datarouter.storage.client.ClientId) CheckResultJspDto(io.datarouter.web.monitoring.latency.CheckResult.CheckResultJspDto)

Aggregations

ClientId (io.datarouter.storage.client.ClientId)21 List (java.util.List)10 Inject (javax.inject.Inject)9 Scanner (io.datarouter.scanner.Scanner)7 BaseHandler (io.datarouter.web.handler.BaseHandler)7 TagCreator.div (j2html.TagCreator.div)6 ContainerTag (j2html.tags.ContainerTag)6 ArrayList (java.util.ArrayList)6 Optional (java.util.Optional)6 DatarouterChangelogDtoBuilder (io.datarouter.instrumentation.changelog.ChangelogRecorder.DatarouterChangelogDtoBuilder)4 DatarouterClients (io.datarouter.storage.client.DatarouterClients)4 PhysicalNode (io.datarouter.storage.node.type.physical.PhysicalNode)4 Mav (io.datarouter.web.handler.mav.Mav)4 J2HtmlTable (io.datarouter.web.html.j2html.J2HtmlTable)4 Bootstrap4PageFactory (io.datarouter.web.html.j2html.bootstrap4.Bootstrap4PageFactory)4 DatarouterWebRequireJsV2 (io.datarouter.web.requirejs.DatarouterWebRequireJsV2)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4