Skip to content

Commit

Permalink
Clean up use of isPresent(). (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Duftler committed Jun 14, 2017
1 parent ec656c0 commit 63000f4
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,25 @@ public TaskResult execute(Stage stage) {
String resolvedStorageAccountName = CredentialsHelper.resolveAccountByNameOrType(storageAccountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedStorageAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedStorageAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to load canary config."));

if (storageService.isPresent()) {
try {
CanaryConfig canaryConfig =
storageService.get().loadObject(resolvedStorageAccountName, ObjectType.CANARY_CONFIG, canaryConfigId.toLowerCase());
try {
CanaryConfig canaryConfig =
storageService.loadObject(resolvedStorageAccountName, ObjectType.CANARY_CONFIG, canaryConfigId.toLowerCase());

// TODO(duftler): Fetch _all_ metric sets specified in canaryConfig.getMetrics(), not just the first.
String metricSetListId = synchronousQueryProcessor.processQuery(resolvedMetricsAccountName,
resolvedStorageAccountName,
canaryConfig.getMetrics().get(0),
atlasCanaryScope);
Map outputs = Collections.singletonMap("metricSetListId", metricSetListId);
// TODO(duftler): Fetch _all_ metric sets specified in canaryConfig.getMetrics(), not just the first.
String metricSetListId = synchronousQueryProcessor.processQuery(resolvedMetricsAccountName,
resolvedStorageAccountName,
canaryConfig.getMetrics().get(0),
atlasCanaryScope);
Map outputs = Collections.singletonMap("metricSetListId", metricSetListId);

return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
throw new IllegalArgumentException("No storage service was configured; unable to load canary config.");
return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Random;

@Component
// TODO(duftler): Make the canary judge a pluggable component.
public class CanaryJudge {
private Random random = new Random();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,16 @@ public TaskResult execute(Stage stage) {
accountCredentialsRepository);
String canaryConfigId = (String)context.get("canaryConfigId");
String metricSetPairListId = (String)context.get("metricSetPairListId");
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to load metric set lists."));

if (storageService.isPresent()) {
CanaryConfig canaryConfig =
storageService.get().loadObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId.toLowerCase());
List<MetricSetPair> metricSetPairList =
storageService.get().loadObject(resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);
float canaryScore = canaryJudge.judge(canaryConfig, metricSetPairList);
Map outputs = Collections.singletonMap("canaryScore", canaryScore);
CanaryConfig canaryConfig = storageService.loadObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId.toLowerCase());
List<MetricSetPair> metricSetPairList = storageService.loadObject(resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId);
float canaryScore = canaryJudge.judge(canaryConfig, metricSetPairList);
Map outputs = Collections.singletonMap("canaryScore", canaryScore);

return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
} else {
throw new IllegalArgumentException("No storage service was configured; unable to load metric set lists.");
}
return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,19 @@ public String processQuery(String metricsAccountName,
String storageAccountName,
CanaryMetricConfig canaryMetricConfig,
CanaryScope canaryScope) throws IOException {
Optional<MetricsService> metricsService = metricsServiceRepository.getOne(metricsAccountName);
List<MetricSet> metricSetList;
MetricsService metricsService =
metricsServiceRepository
.getOne(metricsAccountName)
.orElseThrow(() -> new IllegalArgumentException("No metrics service was configured; unable to read from metrics store."));
List<MetricSet> metricSetList = metricsService.queryMetrics(metricsAccountName, canaryMetricConfig, canaryScope);

if (metricsService.isPresent()) {
metricSetList = metricsService
.get()
.queryMetrics(metricsAccountName, canaryMetricConfig, canaryScope);
} else {
throw new IllegalArgumentException("No metrics service was configured; unable to read from metrics store.");
}

Optional<StorageService> storageService = storageServiceRepository.getOne(storageAccountName);
StorageService storageService =
storageServiceRepository
.getOne(storageAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to write metric set list."));
String metricSetListId = UUID.randomUUID() + "";

if (storageService.isPresent()) {
storageService
.get()
.storeObject(storageAccountName, ObjectType.METRIC_SET_LIST, metricSetListId, metricSetList);
} else {
throw new IllegalArgumentException("No storage service was configured; unable to write metric set list.");
}
storageService.storeObject(storageAccountName, ObjectType.METRIC_SET_LIST, metricSetListId, metricSetList);

return metricSetListId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,23 @@ public TaskResult execute(Stage stage) {
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(storageAccountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to load metric set lists."));

if (storageService.isPresent()) {
List<MetricSet> controlMetricSetList =
storageService.get().loadObject(resolvedAccountName, ObjectType.METRIC_SET_LIST, controlMetricSetListId);
List<MetricSet> experimentMetricSetList =
storageService.get().loadObject(resolvedAccountName, ObjectType.METRIC_SET_LIST, experimentMetricSetListId);
List<MetricSetPair> metricSetPairList =
metricSetMixerService.mixAll(controlMetricSetList, experimentMetricSetList);
String metricSetPairListId = UUID.randomUUID() + "";
List<MetricSet> controlMetricSetList =
storageService.loadObject(resolvedAccountName, ObjectType.METRIC_SET_LIST, controlMetricSetListId);
List<MetricSet> experimentMetricSetList =
storageService.loadObject(resolvedAccountName, ObjectType.METRIC_SET_LIST, experimentMetricSetListId);
List<MetricSetPair> metricSetPairList =
metricSetMixerService.mixAll(controlMetricSetList, experimentMetricSetList);
String metricSetPairListId = UUID.randomUUID() + "";

storageService.get().storeObject(resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId, metricSetPairList);
storageService.storeObject(resolvedAccountName, ObjectType.METRIC_SET_PAIR_LIST, metricSetPairListId, metricSetPairList);

Map outputs = Collections.singletonMap("metricSetPairListId", metricSetPairListId);
Map outputs = Collections.singletonMap("metricSetPairListId", metricSetPairListId);

return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
} else {
throw new IllegalArgumentException("No storage service was configured; unable to load metric set lists.");
}
return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,32 @@ public TaskResult execute(Stage stage) {
String resolvedStorageAccountName = CredentialsHelper.resolveAccountByNameOrType(storageAccountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedStorageAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedStorageAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to load canary config."));

// TODO(duftler): Use orElseThrow() instead.
if (storageService.isPresent()) {
try {
CanaryConfig canaryConfig =
storageService.get().loadObject(resolvedStorageAccountName, ObjectType.CANARY_CONFIG, canaryConfigId.toLowerCase());
try {
CanaryConfig canaryConfig =
storageService.loadObject(resolvedStorageAccountName, ObjectType.CANARY_CONFIG, canaryConfigId.toLowerCase());

Instant startTimeInstant = Instant.parse(stackdriverCanaryScope.getIntervalStartTimeIso());
long startTimeMillis = startTimeInstant.toEpochMilli();
Instant endTimeInstant = Instant.parse(stackdriverCanaryScope.getIntervalEndTimeIso());
long endTimeMillis = endTimeInstant.toEpochMilli();
stackdriverCanaryScope.setStart(startTimeMillis + "");
stackdriverCanaryScope.setEnd(endTimeMillis + "");
Instant startTimeInstant = Instant.parse(stackdriverCanaryScope.getIntervalStartTimeIso());
long startTimeMillis = startTimeInstant.toEpochMilli();
Instant endTimeInstant = Instant.parse(stackdriverCanaryScope.getIntervalEndTimeIso());
long endTimeMillis = endTimeInstant.toEpochMilli();
stackdriverCanaryScope.setStart(startTimeMillis + "");
stackdriverCanaryScope.setEnd(endTimeMillis + "");

// TODO(duftler): Fetch _all_ metric sets specified in canaryConfig.getMetrics(), not just the first.
String metricSetListId = synchronousQueryProcessor.processQuery(resolvedMetricsAccountName,
resolvedStorageAccountName,
canaryConfig.getMetrics().get(0),
stackdriverCanaryScope);
Map outputs = Collections.singletonMap("metricSetListId", metricSetListId);
// TODO(duftler): Fetch _all_ metric sets specified in canaryConfig.getMetrics(), not just the first.
String metricSetListId = synchronousQueryProcessor.processQuery(resolvedMetricsAccountName,
resolvedStorageAccountName,
canaryConfig.getMetrics().get(0),
stackdriverCanaryScope);
Map outputs = Collections.singletonMap("metricSetListId", metricSetListId);

return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
throw new IllegalArgumentException("No storage service was configured; unable to load canary config.");
return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ public CanaryConfig loadCanaryConfig(@RequestParam(required = false) final Strin
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to read canary config from bucket."));

canaryConfigId = canaryConfigId.toLowerCase();

if (storageService.isPresent()) {
return storageService.get().loadObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId);
} else {
log.debug("No storage service was configured; skipping placeholder logic to read from bucket.");
return null;
}
return storageService.loadObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId);
}

@ApiOperation(value = "Write a canary config to object storage")
Expand All @@ -83,7 +81,10 @@ public String storeCanaryConfig(@RequestParam(required = false) final String acc
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to write canary config to bucket."));

if (canaryConfig.getCreatedTimestamp() == null) {
canaryConfig.setCreatedTimestamp(System.currentTimeMillis());
Expand All @@ -104,28 +105,22 @@ public String storeCanaryConfig(@RequestParam(required = false) final String acc
canaryServiceConfig.setName(serviceName);
});

if (storageService.isPresent()) {
String canaryConfigId = canaryConfig.getName().toLowerCase();

if (!canaryConfigIdPattern.matcher(canaryConfigId).matches()) {
throw new IllegalArgumentException("Canary config cannot be named '" + canaryConfigId +
"'. Names must contain only lowercase letters, numbers, dashes (-) and underscores (_).");
}

try {
storageService.get().loadObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId);
} catch (IllegalArgumentException e) {
storageService.get().storeObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId, canaryConfig);
String canaryConfigId = canaryConfig.getName().toLowerCase();

return canaryConfigId;
}
if (!canaryConfigIdPattern.matcher(canaryConfigId).matches()) {
throw new IllegalArgumentException("Canary config cannot be named '" + canaryConfigId +
"'. Names must contain only lowercase letters, numbers, dashes (-) and underscores (_).");
}

throw new IllegalArgumentException("Canary config '" + canaryConfigId + "' already exists.");
} else {
log.debug("No storage service was configured; skipping placeholder logic to write to bucket.");
try {
storageService.loadObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId);
} catch (IllegalArgumentException e) {
storageService.storeObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId, canaryConfig);

return null;
return canaryConfigId;
}

throw new IllegalArgumentException("Canary config '" + canaryConfigId + "' already exists.");
}

@ApiOperation(value = "Delete a canary config")
Expand All @@ -136,9 +131,12 @@ public void deleteCanaryConfig(@RequestParam(required = false) final String acco
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to delete canary config."));

storageService.get().deleteObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId);
storageService.deleteObject(resolvedAccountName, ObjectType.CANARY_CONFIG, canaryConfigId);

response.setStatus(HttpStatus.NO_CONTENT.value());
}
Expand All @@ -149,13 +147,11 @@ public List<Map<String, Object>> listAllCanaryConfigs(@RequestParam(required = f
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to list all canary configs."));

if (storageService.isPresent()) {
return storageService.get().listObjectKeys(resolvedAccountName, ObjectType.CANARY_CONFIG);
} else {
log.debug("No storage service was configured.");
return null;
}
return storageService.listObjectKeys(resolvedAccountName, ObjectType.CANARY_CONFIG);
}
}
Loading

0 comments on commit 63000f4

Please sign in to comment.