use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.
the class S3WebInspector method buildBucketTable.
private ContainerTag<?> buildBucketTable(String contextPath, ClientId clientId) {
DatarouterS3Client client = s3ClientManager.getClient(clientId);
List<S3BucketDto> buckets = client.scanBuckets().parallel(new ParallelScannerContext(bucketRegionExecutor, bucketRegionExecutor.getMaximumPoolSize(), true)).map(bucket -> {
// RPC
Region region = client.getBucketRegion(bucket.name());
return new S3BucketDto(clientId.getName(), bucket.name(), region, bucket.creationDate());
}).sort(Comparator.comparing(bucket -> bucket.bucketName.toLowerCase())).list();
var table = new J2HtmlTable<S3BucketDto>().withClasses("sortable table table-sm table-striped my-4 border").withHtmlColumn("Name", bucket -> {
String href = new URIBuilder().setPath(contextPath + paths.datarouter.clients.awsS3.listObjects.toSlashedString()).addParameter(S3BucketHandler.P_client, bucket.clientName).addParameter(S3BucketHandler.P_bucket, bucket.bucketName).addParameter(S3BucketHandler.P_delimiter, "/").toString();
return td(a(bucket.bucketName).withHref(href));
}).withColumn("Region", bucket -> bucket.region).withColumn("Created", bucket -> bucket.creationDate).build(buckets);
return div(h4("Buckets - " + buckets.size()), table).withClass("container-fluid my-4").withStyle("padding-left: 0px");
}
use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.
the class BaseSchemaUpdateService method acquireSchemaUpdateLock.
private boolean acquireSchemaUpdateLock(Map<ClientId, List<String>> printedSchemaUpdates) {
if (printedSchemaUpdates.isEmpty()) {
return false;
}
String statement = printedSchemaUpdates.entrySet().stream().findFirst().map(entry -> String.join("\n\n", entry.getValue())).get();
Instant now = Instant.now();
Integer build = Optional.ofNullable(buildId).filter(buildId -> !"${env.BUILD_NUMBER}".equals(buildId)).or(() -> Optional.ofNullable(BUILD_NUMBER)).map(Integer::valueOf).orElseGet(() -> (int) now.getEpochSecond());
ClusterSchemaUpdateLock lock = new ClusterSchemaUpdateLock(build, statement, serverName.get(), now);
try {
schemaUpdateLockDao.get().putAndAcquire(lock);
logger.warn("Acquired schema update lock for hash={}", lock.getKey().getStatementHash());
return true;
} catch (Exception ex) {
logger.warn("Didn't acquire schema update lock for hash={}", lock.getKey().getStatementHash());
return false;
}
}
use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.
the class BaseSchemaUpdateService method gatherSchemaUpdates.
public synchronized void gatherSchemaUpdates(boolean wait) {
boolean shouldNotify = true;
Map<ClientId, List<String>> printedSchemaUpdates = new HashMap<>();
Iterator<Future<Optional<SchemaUpdateResult>>> futureIterator = futures.iterator();
MutableString oneStartupBlockReason = new MutableString("");
while (futureIterator.hasNext()) {
Future<Optional<SchemaUpdateResult>> future = futureIterator.next();
if (wait || future.isDone()) {
try {
Optional<SchemaUpdateResult> optional = future.get();
if (optional.isEmpty()) {
continue;
}
printedSchemaUpdates.computeIfAbsent(optional.get().clientId, $ -> new ArrayList<>()).add(optional.get().ddl);
optional.get().startupBlockReason.ifPresent(oneStartupBlockReason::set);
} catch (InterruptedException | ExecutionException e) {
logger.error("", e);
throw new RuntimeException(e);
}
futureIterator.remove();
} else {
shouldNotify = false;
}
}
if (shouldNotify && acquireSchemaUpdateLock(printedSchemaUpdates)) {
sendEmail(printedSchemaUpdates, !oneStartupBlockReason.getString().isEmpty());
recordChangelog(printedSchemaUpdates);
}
if (!oneStartupBlockReason.getString().isEmpty()) {
logger.error(oneStartupBlockReason.getString());
throw new RuntimeException(oneStartupBlockReason.getString());
}
}
use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.
the class SqsQueueRegistryService method getSqsQueuesForClient.
public Pair<List<Twin<String>>, List<String>> getSqsQueuesForClient(ClientId clientId) {
Set<String> knownQueuesUrls = new HashSet<>();
AmazonSQS sqs = sqsClientManager.getAmazonSqs(clientId);
List<? extends BaseSqsNode<?, ?, ?>> sqsNodes = Scanner.of(nodes.getPhysicalNodesForClient(clientId.getName())).map(NodeTool::extractSinglePhysicalNode).map(physicalNode -> (BaseSqsNode<?, ?, ?>) physicalNode).list();
List<Twin<String>> knownQueueUrlByName = Scanner.of(sqsNodes).map(BaseSqsNode::getQueueUrlAndName).map(Supplier::get).each(twin -> knownQueuesUrls.add(twin.getLeft())).list();
List<String> unreferencedQueues = Scanner.of(sqsNodes).map(BaseSqsNode::buildNamespace).distinct().map(sqs::listQueues).concatIter(ListQueuesResult::getQueueUrls).exclude(knownQueuesUrls::contains).map(queueUrl -> StringTool.getStringAfterLastOccurrence("/", queueUrl)).include(queueName -> sqsQueueExists(sqs, queueName)).list();
return new Pair<>(knownQueueUrlByName, unreferencedQueues);
}
use of io.datarouter.storage.client.ClientId in project datarouter by hotpads.
the class SqsUpdateQueueHandler method deleteAllUnreferencedQueues.
@Handler
private Mav deleteAllUnreferencedQueues(@Param(PARAM_clientName) String clientName, @Param(PARAM_referer) String referer) {
ClientId clientId = datarouterClients.getClientId(clientName);
List<String> unreferencedQueueNames = queueRegistryService.getSqsQueuesForClient(clientId).getRight();
AmazonSQS sqs = sqsClientManager.getAmazonSqs(clientId);
Scanner.of(unreferencedQueueNames).map(sqs::getQueueUrl).map(GetQueueUrlResult::getQueueUrl).forEach(sqs::deleteQueue);
String message = "Deleted all unreferenced SQS queues";
Scanner.of(unreferencedQueueNames).map(queueName -> new DatarouterChangelogDtoBuilder("Sqs", queueName, "delete unreferenced queue", getSessionInfo().getRequiredSession().getUsername())).map(DatarouterChangelogDtoBuilder::build).forEach(changelogRecorder::record);
return buildPage(referer, message);
}
Aggregations