Skip to content

Commit

Permalink
improved logging messages in AWSDynamoDAO
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed May 21, 2019
1 parent 5fccfb7 commit 1b818f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Expand Up @@ -173,13 +173,14 @@ private String createRow(String key, String appid, Map<String, AttributeValue> r
if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) {
return null;
}
String table = getTableNameForAppid(appid);
try {
key = getKeyForAppid(key, appid);
setRowKey(key, row);
PutItemRequest putItemRequest = new PutItemRequest(getTableNameForAppid(appid), row);
PutItemRequest putItemRequest = new PutItemRequest(table, row);
client().putItem(putItemRequest);
} catch (Exception e) {
logger.error("Could not write row to DB - appid={}, key={}", appid, key, e);
logger.error("Could not write row to DB - table={}, appid={}, key={}", table, appid, key, e);
throwIfNecessary(e);
}
return key;
Expand All @@ -189,6 +190,7 @@ private boolean updateRow(String key, String appid, Map<String, AttributeValue>
if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) {
return false;
}
String table = getTableNameForAppid(appid);
try {
UpdateItemRequest updateRequest = new UpdateItemRequest();
StringBuilder updateExpression = new StringBuilder("SET ");
Expand Down Expand Up @@ -219,17 +221,17 @@ private boolean updateRow(String key, String appid, Map<String, AttributeValue>
updateExpression.append(" ADD #").append(Config._VERSION).append(" :plusOne");
}

updateRequest.setTableName(getTableNameForAppid(appid));
updateRequest.setTableName(table);
updateRequest.setKey(Collections.singletonMap(Config._KEY, new AttributeValue(getKeyForAppid(key, appid))));
updateRequest.setExpressionAttributeNames(names);
updateRequest.setExpressionAttributeValues(values);
updateRequest.setUpdateExpression(updateExpression.toString());
client().updateItem(updateRequest);
return true;
} catch (ConditionalCheckFailedException ex) {
logger.warn("Item not updated. Versions don't match. Appid={}, key={}.", appid, key);
logger.warn("Item not updated - versions don't match. table={}, appid={}, key={}.", table, appid, key);
} catch (Exception e) {
logger.error("Could not update row in DB - appid={}, key={}", appid, key, e);
logger.error("Could not update row in DB - table={}, appid={}, key={}", table, appid, key, e);
throwIfNecessary(e);
}
return false;
Expand All @@ -240,15 +242,16 @@ private Map<String, AttributeValue> readRow(String key, String appid) {
return null;
}
Map<String, AttributeValue> row = null;
String table = getTableNameForAppid(appid);
try {
GetItemRequest getItemRequest = new GetItemRequest(getTableNameForAppid(appid),
GetItemRequest getItemRequest = new GetItemRequest(table,
Collections.singletonMap(Config._KEY, new AttributeValue(getKeyForAppid(key, appid))));
GetItemResult res = client().getItem(getItemRequest);
if (res != null && res.getItem() != null && !res.getItem().isEmpty()) {
row = res.getItem();
}
} catch (Exception e) {
logger.error("Could not read row from DB - appid={}, key={}", appid, key, e);
logger.error("Could not read row from DB - table={}, appid={}, key={}", appid, key, e);
}
return (row == null || row.isEmpty()) ? null : row;
}
Expand All @@ -257,12 +260,13 @@ private void deleteRow(String key, String appid) {
if (StringUtils.isBlank(key) || StringUtils.isBlank(appid)) {
return;
}
String table = getTableNameForAppid(appid);
try {
DeleteItemRequest delItemRequest = new DeleteItemRequest(getTableNameForAppid(appid),
DeleteItemRequest delItemRequest = new DeleteItemRequest(table,
Collections.singletonMap(Config._KEY, new AttributeValue(getKeyForAppid(key, appid))));
client().deleteItem(delItemRequest);
} catch (Exception e) {
logger.error("Could not delete row from DB - appid={}, key={}", appid, key, e);
logger.error("Could not delete row from DB - table={}, appid={}, key={}", table, appid, key, e);
throwIfNecessary(e);
}
}
Expand Down Expand Up @@ -325,7 +329,7 @@ public <P extends ParaObject> Map<String, P> readAll(String appid, List<String>

Map<String, P> results = new LinkedHashMap<>(keySet.size(), 0.75f, true);
ArrayList<Map<String, AttributeValue>> keyz = new ArrayList<>(MAX_KEYS_PER_READ);

String table = getTableNameForAppid(appid);
try {
int batchSteps = 1;
if ((keySet.size() > MAX_KEYS_PER_READ)) {
Expand All @@ -334,7 +338,6 @@ public <P extends ParaObject> Map<String, P> readAll(String appid, List<String>
}

Iterator<String> it = keySet.iterator();
String tableName = getTableNameForAppid(appid);
int j = 0;

for (int i = 0; i < batchSteps; i++) {
Expand All @@ -350,13 +353,13 @@ public <P extends ParaObject> Map<String, P> readAll(String appid, List<String>
kna.setAttributesToGet(Arrays.asList(Config._ID, Config._KEY, Config._TYPE));
}

batchGet(Collections.singletonMap(tableName, kna), results);
batchGet(Collections.singletonMap(table, kna), results);
keyz.clear();
j = 0;
}
logger.debug("DAO.readAll({}) {}", keySet, results.size());
} catch (Exception e) {
logger.error("Failed to readAll({}): {}", keys, e);
logger.error("Failed to readAll({}), table={}: {}", keys, table, e);
}
return results;
}
Expand All @@ -370,6 +373,7 @@ public <P extends ParaObject> List<P> readPage(String appid, Pager pager) {
pager = new Pager();
}
List<P> results = new LinkedList<>();
String table = getTableNameForAppid(appid);
try {
if (isSharedAppid(appid)) {
results = readPageFromSharedTable(appid, pager);
Expand All @@ -378,7 +382,7 @@ public <P extends ParaObject> List<P> readPage(String appid, Pager pager) {
}
pager.setCount(pager.getCount() + results.size());
} catch (Exception e) {
logger.error("Failed to readPage({}): {}", appid, e);
logger.error("Failed to readPage({}), table={}: {}", appid, table, e);
}
return results;
}
Expand Down
Expand Up @@ -441,7 +441,7 @@ protected static <P extends ParaObject> void batchGet(Map<String, KeysAndAttribu
batchGet(result.getUnprocessedKeys(), results);
}
} catch (Exception e) {
logger.error(null, e);
logger.error("Failed to execute batch read operation on table '{}'", kna.keySet().iterator().next(), e);
}
}

Expand All @@ -468,7 +468,7 @@ protected static void batchWrite(Map<String, List<WriteRequest>> items, int back
batchWrite(result.getUnprocessedItems(), backoff * 2);
}
} catch (Exception e) {
logger.error(null, e);
logger.error("Failed to execute batch write operation on table '{}'", items.keySet().iterator().next(), e);
throwIfNecessary(e);
}
}
Expand Down

0 comments on commit 1b818f0

Please sign in to comment.