Skip to content

Commit

Permalink
Merge pull request #43 from yammer/azure_storage
Browse files Browse the repository at this point in the history
Azure storage
  • Loading branch information
chrisgray authored Aug 4, 2017
2 parents 6126de7 + c11b3ef commit a62ceae
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next
---
* [Removed hardcoding of rancher query parameters](https://github.com/yammer/breakerbox/pull/42) Thanks @priyadarsh
* [Dropwizard 1.1.3](https://github.com/dropwizard/dropwizard/releases/tag/v1.1.3)
* [azure-storage-java 5.4.0](https://github.com/Azure/azure-storage-java/releases/tag/v5.4.0)

0.6.3 -- July 13, 2017
---
Expand Down
4 changes: 2 additions & 2 deletions breakerbox-azure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<artifactId>breakerbox-store</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.windowsazure</groupId>
<artifactId>microsoft-windowsazure-api</artifactId>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.microsoft.windowsazure.services.table.client.TableConstants;
import com.microsoft.windowsazure.services.table.client.TableQuery;
import com.microsoft.azure.storage.table.TableQuery;
import com.yammer.breakerbox.azure.core.TableId;
import com.yammer.breakerbox.azure.core.TableType;
import com.yammer.breakerbox.azure.healthchecks.TableClientHealthcheck;
Expand All @@ -23,6 +22,8 @@
import org.joda.time.DateTime;

public class AzureStore extends BreakerboxStore {
public static final String PARTITION_KEY = "PartitionKey";
public static final String ROW_KEY = "RowKey";
protected final TableClient tableClient;

public AzureStore(AzureTableConfiguration azureTableConfiguration,
Expand Down Expand Up @@ -102,8 +103,9 @@ public Iterable<ServiceModel> listDependenciesFor(final ServiceId serviceId) {
@Override
public Iterable<DependencyModel> allDependenciesFor(DependencyId dependencyId, ServiceId serviceId) {
try (Timer.Context timerContext = dependencyConfigs.time()) {
return Entities.toDependencyModelList(tableClient.search(TableQuery
.from(TableId.DEPENDENCY.toString(), DependencyEntity.class)
return Entities.toDependencyModelList(tableClient.search(
TableId.DEPENDENCY, TableQuery
.from(DependencyEntity.class)
.where(TableQuery.combineFilters(
partitionEquals(dependencyId),
TableQuery.Operators.AND,
Expand Down Expand Up @@ -134,31 +136,31 @@ private Optional<DependencyEntity> fetchByTimestamp(DependencyId dependencyId, l

private ImmutableList<ServiceEntity> allServiceEntities(ServiceId serviceId) {
try (Timer.Context timerContext = listService.time()) {
return tableClient.search(TableQuery
.from(TableId.SERVICE.toString(), ServiceEntity.class)
return tableClient.search(TableId.SERVICE, TableQuery
.from(ServiceEntity.class)
.where(partitionKeyEquals(serviceId)));
}
}

private static String partitionKeyEquals(ServiceId serviceId) {
return TableQuery
.generateFilterCondition(
TableConstants.PARTITION_KEY,
PARTITION_KEY,
TableQuery.QueryComparisons.EQUAL,
serviceId.getId());
}

private ImmutableList<ServiceEntity> allServiceEntities() {
try (Timer.Context timerContext = listService.time()) {
return tableClient.search(TableQuery
.from(TableId.SERVICE.toString(), ServiceEntity.class));
return tableClient.search(TableId.SERVICE, TableQuery
.from(ServiceEntity.class));
}
}

private ImmutableList<DependencyEntity> getConfiguration(DependencyId dependencyId, long targetTimeStamp) {
try (Timer.Context timerContext = dependencyConfigs.time()) {
return tableClient.search(TableQuery
.from(TableId.DEPENDENCY.toString(), DependencyEntity.class)
return tableClient.search(TableId.DEPENDENCY, TableQuery
.from(DependencyEntity.class)
.where(TableQuery.combineFilters(
partitionEquals(dependencyId),
TableQuery.Operators.AND,
Expand All @@ -175,15 +177,15 @@ private static String serviceIdEquals(ServiceId serviceId) {

private static String timestampEquals(long timestamp) {
return TableQuery.generateFilterCondition(
TableConstants.ROW_KEY,
ROW_KEY,
TableQuery.QueryComparisons.EQUAL,
String.valueOf(timestamp)
);
}

private static String partitionEquals(DependencyId dependencyId) {
return TableQuery.generateFilterCondition(
TableConstants.PARTITION_KEY,
PARTITION_KEY,
TableQuery.QueryComparisons.EQUAL,
dependencyId.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.microsoft.windowsazure.services.core.storage.StorageCredentialsAccountAndKey;
import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
import io.dropwizard.util.Duration;

import javax.validation.Valid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.microsoft.windowsazure.services.core.storage.StorageException;
import com.microsoft.windowsazure.services.table.client.*;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.table.*;
import com.yammer.breakerbox.azure.core.AzureTableName;
import com.yammer.breakerbox.azure.core.TableKey;
import com.yammer.breakerbox.azure.core.TableType;
Expand All @@ -24,13 +24,21 @@ public TableClient(CloudTableClient cloudTableClient) {
this.cloudTableClient = checkNotNull(cloudTableClient, "cloudTableClient cannot be null");
}

private CloudTable tableRefrence(AzureTableName table) {
try {
return cloudTableClient.getTableReference(table.toString());
} catch (URISyntaxException e) {
LOG.warn("URI exception creating table: {},", table, e);
} catch (StorageException e) {
LOG.warn("Error generating TableClient: {}", table, e);
}
throw new IllegalStateException("Could not create table: " + table);
}

public void create(AzureTableName table) {
try {
final CloudTable cloudTable = new CloudTable(table.toString(), cloudTableClient);
cloudTable.createIfNotExist();
tableRefrence(table).createIfNotExists();
return;
} catch (URISyntaxException e) {
LOG.warn("URI exception creating table", e);
} catch (StorageException e) {
LOG.warn("Error creating table: {}", table, e);
}
Expand All @@ -39,9 +47,9 @@ public void create(AzureTableName table) {

public boolean insert(TableType entity) {
try {
final TableResult tableResult = cloudTableClient.execute(
entity.getAzureTableName().toString(), TableOperation.insert(entity));
return tableResult.getHttpStatusCode() == Response.Status.CREATED.getStatusCode();
return tableRefrence(entity.getAzureTableName())
.execute(TableOperation.insert(entity))
.getHttpStatusCode() == Response.Status.NO_CONTENT.getStatusCode();
} catch (StorageException e) {
LOG.warn("Error performing operation on Storage service", e);
}
Expand All @@ -50,8 +58,8 @@ public boolean insert(TableType entity) {

public boolean insertOrReplace(TableType entity) {
try {
final TableResult tableResult = cloudTableClient.execute(
entity.getAzureTableName().toString(), TableOperation.insertOrReplace(entity));
final TableResult tableResult = tableRefrence(entity.getAzureTableName())
.execute(TableOperation.insertOrReplace(entity));
switch (Response.Status.fromStatusCode(tableResult.getHttpStatusCode())) {
case CREATED:
case NO_CONTENT:
Expand All @@ -67,12 +75,9 @@ public boolean insertOrReplace(TableType entity) {

public <EntityType extends TableServiceEntity> Optional<EntityType> retrieve(TableKey tableKey) {
try {
final TableResult tableResult = cloudTableClient.execute(
tableKey.getTable().toString(),
TableOperation.retrieve(
tableKey.getPartitionKey(), tableKey.getRowKey(), tableKey.getEntityClass()));
final EntityType entityType = tableResult.getResultAsType();
return Optional.fromNullable(entityType);
return Optional.fromNullable(tableRefrence(tableKey.getTable())
.execute(TableOperation.retrieve(tableKey.getPartitionKey(), tableKey.getRowKey(), tableKey.getEntityClass()))
.getResultAsType());
} catch (StorageException e) {
LOG.warn("Error retrieving entity from table: {}", tableKey.getTable(), e);
}
Expand All @@ -81,10 +86,9 @@ public <EntityType extends TableServiceEntity> Optional<EntityType> retrieve(Tab

public boolean update(TableType entity) {
try {
final TableResult result = cloudTableClient.execute(
entity.getAzureTableName().toString(),
TableOperation.replace(entity));
return result.getHttpStatusCode() == Response.Status.NO_CONTENT.getStatusCode();
return tableRefrence(entity.getAzureTableName())
.execute(TableOperation.replace(entity))
.getHttpStatusCode() == Response.Status.NO_CONTENT.getStatusCode();
} catch (StorageException e) {
LOG.warn("Error updating row in table: {}", entity.getAzureTableName(), e);
}
Expand All @@ -93,10 +97,7 @@ public boolean update(TableType entity) {

public boolean destroy(AzureTableName azureTableName) {
try {
final CloudTable cloudTable = new CloudTable(azureTableName.toString(), cloudTableClient);
return cloudTable.deleteIfExists();
} catch (URISyntaxException e) {
LOG.warn("Invalid Azure table URL specified {}", azureTableName, e);
return tableRefrence(azureTableName).deleteIfExists();
} catch (StorageException e) {
LOG.warn("Error deleting table from Azure", e);
}
Expand All @@ -105,18 +106,16 @@ public boolean destroy(AzureTableName azureTableName) {

public boolean exists(AzureTableName azureTableName) {
try {
final CloudTable cloudTable = new CloudTable(azureTableName.toString(), cloudTableClient);
return cloudTable.exists();
} catch (URISyntaxException e) {
LOG.warn("Invalid Azure table URL specified {}", azureTableName, e);
return tableRefrence(azureTableName).exists();
} catch (StorageException e) {
LOG.warn("Error accessing azure table", e);
}
throw new IllegalStateException("Error verifying if table " + azureTableName + " exists");
}

public <EntityType extends TableEntity> ImmutableList<EntityType> search(TableQuery<EntityType> query) {
return ImmutableList.copyOf(cloudTableClient.execute(query));
public <EntityType extends TableEntity> ImmutableList<EntityType> search(AzureTableName azureTableName,
TableQuery<EntityType> query) {
return ImmutableList.copyOf(tableRefrence(azureTableName).execute(query));
}

public Iterable<String> listTables() {
Expand All @@ -125,9 +124,9 @@ public Iterable<String> listTables() {

public boolean remove(TableType entity) {
try {
final TableResult result = cloudTableClient.execute(
entity.getAzureTableName().toString(), TableOperation.delete(entity));
return result.getHttpStatusCode() == Response.Status.NO_CONTENT.getStatusCode();
return tableRefrence(entity.getAzureTableName())
.execute(TableOperation.delete(entity))
.getHttpStatusCode() == Response.Status.NO_CONTENT.getStatusCode();
} catch (StorageException e) {
LOG.warn("Error updating row in table: {}", entity.getAzureTableName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.yammer.breakerbox.azure;

import com.microsoft.windowsazure.services.core.storage.CloudStorageAccount;
import com.microsoft.windowsazure.services.core.storage.RetryLinearRetry;
import com.microsoft.windowsazure.services.table.client.CloudTableClient;
import com.google.common.primitives.Ints;
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.RetryLinearRetry;
import com.microsoft.azure.storage.table.CloudTableClient;
import com.microsoft.azure.storage.table.TableRequestOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -23,11 +25,12 @@ public TableClient create() {
final CloudStorageAccount storageAccount =
new CloudStorageAccount(azureTableConfiguration.getStorageCredentialsAccountAndKey(), true);
final CloudTableClient cloudTableClient = storageAccount.createCloudTableClient();
cloudTableClient.setRetryPolicyFactory(
new RetryLinearRetry(
(int) azureTableConfiguration.getRetryInterval().toMilliseconds(),
azureTableConfiguration.getRetryAttempts()));
cloudTableClient.setTimeoutInMs((int) azureTableConfiguration.getTimeout().toMilliseconds());
final TableRequestOptions defaultOptions = new TableRequestOptions();
defaultOptions.setRetryPolicyFactory(new RetryLinearRetry(
Ints.checkedCast(azureTableConfiguration.getRetryInterval().toMilliseconds()),
azureTableConfiguration.getRetryAttempts()));
defaultOptions.setTimeoutIntervalInMs(Ints.checkedCast(azureTableConfiguration.getTimeout().toMilliseconds()));
cloudTableClient.setDefaultRequestOptions(defaultOptions);
return new TableClient(cloudTableClient);
} catch (URISyntaxException err) {
LOGGER.error("Failed to create a TableClient", err);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yammer.breakerbox.azure.core;

import com.microsoft.windowsazure.services.table.client.TableServiceEntity;
import com.microsoft.azure.storage.table.TableServiceEntity;

public interface TableKey {
String getRowKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yammer.breakerbox.azure.core;

import com.microsoft.windowsazure.services.table.client.TableServiceEntity;
import com.microsoft.azure.storage.table.TableServiceEntity;

import static com.google.common.base.Preconditions.checkNotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.microsoft.windowsazure.services.table.client.TableServiceEntity;
import com.microsoft.azure.storage.table.TableServiceEntity;
import com.yammer.breakerbox.azure.core.AzureTableName;
import com.yammer.breakerbox.azure.core.TableId;
import com.yammer.breakerbox.azure.core.TableKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yammer.breakerbox.azure.model;

import com.microsoft.windowsazure.services.table.client.TableServiceEntity;
import com.microsoft.azure.storage.table.TableServiceEntity;
import com.yammer.breakerbox.azure.core.AzureTableName;
import com.yammer.breakerbox.azure.core.TableId;
import com.yammer.breakerbox.azure.core.TableKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.yammer.breakerbox.azure.model.tests;

import com.google.common.base.Optional;
import com.microsoft.windowsazure.services.table.client.TableServiceEntity;
import com.microsoft.azure.storage.table.TableServiceEntity;
import com.yammer.breakerbox.azure.TableClient;
import com.yammer.breakerbox.azure.TableClientFactory;
import com.yammer.breakerbox.azure.model.DependencyEntity;
Expand Down
22 changes: 4 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<properties>
<dropwizard.version>1.1.3</dropwizard.version>
<windowsazure.version>0.4.6</windowsazure.version>
<azure-storage.version>5.4.0</azure-storage.version>
<tenacity.version>1.1.3</tenacity.version>
<turbine.version>1.0.0</turbine.version>
<dropwizard-auth-ldap.version>1.0.5</dropwizard-auth-ldap.version>
Expand Down Expand Up @@ -117,23 +117,9 @@
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.windowsazure</groupId>
<artifactId>microsoft-windowsazure-api</artifactId>
<version>${windowsazure.version}</version>
<exclusions>
<exclusion>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
</exclusion>
</exclusions>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>${azure-storage.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
Expand Down

0 comments on commit a62ceae

Please sign in to comment.