use of com.microsoft.azure.storage.ResultContinuation in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method getStorageContainersAsync.
/*
* Get all Azure containers by storage account
*/
public void getStorageContainersAsync(StorageEnumContext context, StorageEnumStages next) {
if (context.storageAccountIds.size() == 0) {
logFine(() -> "No storage description available - clean up all resources");
context.subStage = StorageEnumStages.DISASSOCIATE_RESOURCE_GROUP_STATES;
handleSubStage(context);
return;
}
Consumer<Throwable> failure = e -> {
logWarning("Failure getting Azure storage containers [EndpointLink:%s] [Exception:%s]", context.request.endpointLink, e.getMessage());
handleError(context, e);
};
PhotonModelUtils.runInExecutor(this.executorService, () -> {
for (String id : context.storageAccountIds) {
String storageConnectionString = context.storageConnectionStrings.get(id);
if (storageConnectionString == null) {
continue;
}
try {
CloudStorageAccount storageAccount;
if (AzureUtils.isAzureClientMock()) {
StorageCredentials credentials = StorageCredentials.tryParseCredentials(storageConnectionString);
storageAccount = new CloudStorageAccount(credentials, new URI(AzureUtils.getAzureBaseUri()), new URI(AzureUtils.getAzureBaseUri()), new URI(AzureUtils.getAzureBaseUri()), new URI(AzureUtils.getAzureBaseUri()));
} else {
storageAccount = CloudStorageAccount.parse(storageConnectionString);
}
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
ResultContinuation nextContainerResults = null;
do {
try {
ResultSegment<CloudBlobContainer> contSegment = blobClient.listContainersSegmented(null, ContainerListingDetails.NONE, getQueryResultLimit(), nextContainerResults, null, null);
context.apiListStorageContainers++;
nextContainerResults = contSegment.getContinuationToken();
for (CloudBlobContainer container : contSegment.getResults()) {
String uri = canonizeId(container.getUri().toString());
context.containerIds.add(uri);
context.storageContainers.put(uri, container);
ResultContinuation nextBlobResults = null;
do {
ResultSegment<ListBlobItem> blobsSegment = container.listBlobsSegmented(null, false, EnumSet.noneOf(BlobListingDetails.class), getQueryResultLimit(), nextBlobResults, null, null);
context.apiListBlobs++;
nextBlobResults = blobsSegment.getContinuationToken();
for (ListBlobItem blobItem : blobsSegment.getResults()) {
String blobId = canonizeId(blobItem.getUri().toString());
context.storageBlobs.put(blobId, blobItem);
// populate mapping of blob uri and storage account for all storage
// accounts as new disks can be added to already existing blobs
StorageAccount blobStorageAccount = context.storageAccountMap.get(id);
if (blobStorageAccount != null) {
context.storageAccountBlobUriMap.put(blobId, blobStorageAccount);
}
context.blobIds.add(blobId);
}
} while (nextBlobResults != null);
}
} catch (StorageException storageExc) {
if (storageExc.getCause() instanceof UnknownHostException || StorageErrorCode.RESOURCE_NOT_FOUND.toString().equals(storageExc.getErrorCode()) || AzureConstants.RESOURCE_NOT_FOUND.equals(storageExc.getErrorCode())) {
String msg = "Probably trying to process a storage account/container that was " + "just deleted. Skipping it and continue with the next " + "storage account. Storage account id: [" + id + "], " + "storage account connection string: [" + storageConnectionString + "]. Error: %s";
logInfo(msg, Utils.toString(storageExc));
} else {
logSevere("StorageException[errorCode=%s, httpCode=%s, msg=%s, cause=%s]", storageExc.getErrorCode(), storageExc.getHttpStatusCode(), storageExc.getMessage(), storageExc.getCause() != null ? Utils.toString(storageExc.getCause()) : "n/a");
throw storageExc;
}
}
} while (nextContainerResults != null);
logFine(() -> String.format("Processing %d storage containers", context.containerIds.size()));
} catch (Exception e) {
handleError(context, e);
return;
}
}
context.subStage = next;
handleSubStage(context);
}, failure);
}
use of com.microsoft.azure.storage.ResultContinuation in project photon-model by vmware.
the class AzureComputeHostStorageStatsGatherer method getBlobUsedBytesAsync.
private void getBlobUsedBytesAsync(AzureStorageStatsDataHolder statsData, StorageMetricsStages next) {
Runnable getBlobsAsync = () -> {
String metricName = PhotonModelConstants.STORAGE_USED_BYTES;
List<ServiceStats.ServiceStat> statDatapoints = new ArrayList<>();
AtomicInteger accountsCount = new AtomicInteger(statsData.storageAccounts.size());
final List<Throwable> exs = new ArrayList<>();
for (Map.Entry<String, StorageAccount> account : statsData.storageAccounts.entrySet()) {
String resourceGroupName = getResourceGroupName(account.getValue().id);
statsData.azureClients.getAzureClient().storageAccounts().inner().listKeysAsync(resourceGroupName, account.getValue().name, new AzureAsyncCallback<StorageAccountListKeysResultInner>() {
@Override
public void onError(Throwable e) {
handleError(statsData, e);
}
@Override
public void onSuccess(StorageAccountListKeysResultInner result) {
logFine(() -> String.format("Retrieved the storage account keys for" + " storage account [%s].", account.getValue().name));
String storageConnectionString = String.format(STORAGE_CONNECTION_STRING, account.getValue().name, result.keys().get(0).value());
try {
CloudStorageAccount storageAccount = getAzureStorageClient(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
ResultContinuation nextContainerResults = null;
do {
ResultSegment<CloudBlobContainer> contSegment = blobClient.listContainersSegmented(null, ContainerListingDetails.NONE, QUERY_RESULT_LIMIT, nextContainerResults, null, null);
nextContainerResults = contSegment.getContinuationToken();
for (CloudBlobContainer container : contSegment.getResults()) {
ResultContinuation nextBlobResults = null;
do {
ResultSegment<ListBlobItem> blobsSegment = container.listBlobsSegmented(null, false, EnumSet.noneOf(BlobListingDetails.class), QUERY_RESULT_LIMIT, nextBlobResults, null, null);
nextBlobResults = blobsSegment.getContinuationToken();
for (ListBlobItem blobItem : blobsSegment.getResults()) {
if (blobItem instanceof CloudPageBlob) {
CloudPageBlob pageBlob = (CloudPageBlob) blobItem;
// TODO https://jira-hzn.eng.vmware.com/browse/VSYM-3445
try {
CloudBlob blobSnapshot = pageBlob.createSnapshot();
statsData.snapshots.add(blobSnapshot);
CloudPageBlob pageBlobSnapshot = (CloudPageBlob) blobSnapshot;
ArrayList<PageRange> pages = pageBlobSnapshot.downloadPageRanges();
// https://jira-hzn.eng.vmware.com/browse/VSYM-3355
for (PageRange pageRange : pages) {
statsData.utilizedBytes += pageRange.getEndOffset() - pageRange.getStartOffset();
}
} catch (StorageException e) {
logWarning(() -> String.format("Error getting blob size: [%s]", e.getMessage()));
}
}
}
} while (nextBlobResults != null);
}
} while (nextContainerResults != null);
} catch (Exception e) {
logWarning(() -> String.format("Exception while getting blob used bytes: %s", Utils.toString(e)));
exs.add(e);
} finally {
// in the Azure account
if (statsData.snapshots.size() > 0) {
synchronized (statsData.snapshots) {
Iterator<CloudBlob> snapshotIterator = statsData.snapshots.iterator();
while (snapshotIterator.hasNext()) {
try {
CloudBlob snapshot = snapshotIterator.next();
snapshot.deleteIfExists();
snapshotIterator.remove();
} catch (StorageException e) {
// Best effort to delete all the snapshots
logWarning(() -> String.format("Exception while deleting snapshot: %s", Utils.toString(e)));
}
}
}
}
}
// if all storage accounts were processed, create ServiceStat and finish
if (accountsCount.decrementAndGet() == 0) {
if (!exs.isEmpty()) {
handleError(statsData, exs.iterator().next());
return;
}
if (statsData.utilizedBytes != 0) {
ServiceStats.ServiceStat stat = new ServiceStats.ServiceStat();
stat.latestValue = statsData.utilizedBytes;
stat.sourceTimeMicrosUtc = TimeUnit.MILLISECONDS.toMicros(Utils.getNowMicrosUtc());
stat.unit = PhotonModelConstants.getUnitForMetric(metricName);
statDatapoints.add(stat);
}
statsData.statsResponse.statValues.put(metricName, statDatapoints);
if (statsData.statsResponse.statValues.size() == 1) {
statsData.statsResponse.computeLink = statsData.computeHostDesc.documentSelfLink;
}
statsData.stage = next;
handleStorageMetricDiscovery(statsData);
}
}
});
}
};
PhotonModelUtils.runInExecutor(this.executorService, getBlobsAsync, throwable -> handleError(statsData, throwable));
}
Aggregations