Search in sources :

Example 6 with QueryOutcome

use of com.amazonaws.services.dynamodbv2.document.QueryOutcome in project aws-doc-sdk-examples by awsdocs.

the class TryDaxTests method queryTest.

void queryTest(String tableName, DynamoDB client, int pk, int sk1, int sk2, int iterations) {
    long startTime, endTime;
    System.out.println("Query test - partition key " + pk + " and sort keys between " + sk1 + " and " + sk2);
    Table table = client.getTable(tableName);
    HashMap<String, Object> valueMap = new HashMap<String, Object>();
    valueMap.put(":pkval", pk);
    valueMap.put(":skval1", sk1);
    valueMap.put(":skval2", sk2);
    QuerySpec spec = new QuerySpec().withKeyConditionExpression("pk = :pkval and sk between :skval1 and :skval2").withValueMap(valueMap);
    for (int i = 0; i < iterations; i++) {
        startTime = System.nanoTime();
        ItemCollection<QueryOutcome> items = table.query(spec);
        try {
            Iterator<Item> iter = items.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
        } catch (Exception e) {
            System.err.println("Unable to query table:");
            e.printStackTrace();
        }
        endTime = System.nanoTime();
        printTime(startTime, endTime, iterations);
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) HashMap(java.util.HashMap) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) Item(com.amazonaws.services.dynamodbv2.document.Item) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 7 with QueryOutcome

use of com.amazonaws.services.dynamodbv2.document.QueryOutcome in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnection method mostUpdatedHostRecord.

private boolean mostUpdatedHostRecord(Item recordToCheck) {
    try {
        // Query all records with the same hostName / provider / service as recordToCheck
        QuerySpec spec = new QuerySpec().withKeyConditionExpression("hostName = :v_host_name").withFilterExpression("attribute_exists(provider) AND provider = :v_provider AND attribute_exists(service) AND service = :v_service").withValueMap(new ValueMap().withString(":v_host_name", recordToCheck.getString(KEY_HOSTNAME)).withString(":v_provider", recordToCheck.getString(KEY_PROVIDER)).withString(":v_service", recordToCheck.getString(KEY_SERVICE)));
        ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> hostNameIndex.query(spec));
        List<Item> allRecordsWithHost = new ArrayList<>();
        for (Item item : outcome) {
            allRecordsWithHost.add(item);
        }
        // Verify recordToCheck is the most updated record with this hostName
        return dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(recordToCheck, allRecordsWithHost, KEY_CURRENT_TIME, KEY_PRIMARY);
    } catch (Exception ex) {
        LOGGER.error("DynamoDB mostUpdatedHostRecord failed for item: {}, error: {}", recordToCheck.toString(), ex.getMessage());
        return false;
    }
}
Also used : ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 8 with QueryOutcome

use of com.amazonaws.services.dynamodbv2.document.QueryOutcome in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnection method getUnrefreshedCertRecordsByDate.

private List<Item> getUnrefreshedCertRecordsByDate(String provider, long yesterday, String unrefreshedCertDate) {
    try {
        QuerySpec spec = new QuerySpec().withKeyConditionExpression("currentDate = :v_current_date").withFilterExpression("provider = :v_provider AND attribute_exists(hostName) AND (attribute_not_exists(lastNotifiedTime) OR lastNotifiedTime < :v_last_notified)").withValueMap(new ValueMap().withString(":v_current_date", unrefreshedCertDate).withNumber(":v_last_notified", yesterday).withString(":v_provider", provider));
        ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> currentTimeIndex.query(spec));
        List<Item> items = new ArrayList<>();
        for (Item item : outcome) {
            items.add(item);
        }
        return items;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB getUnrefreshedCertRecordsByDate failed for provider: {}, date: {} error: {}", provider, unrefreshedCertDate, ex.getMessage());
    }
    return new ArrayList<>();
}
Also used : ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 9 with QueryOutcome

use of com.amazonaws.services.dynamodbv2.document.QueryOutcome in project athenz by yahoo.

the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByIp.

@Test
public void testGetWorkloadRecordsByIp() {
    long currTime = System.currentTimeMillis();
    Mockito.doReturn("1234").when(item).getString("instanceId");
    Mockito.doReturn("openstack").when(item).getString("provider");
    Mockito.doReturn("athenz.api").when(item).getString("service");
    Mockito.doReturn("test-host").when(item).getString("hostname");
    Mockito.doReturn(currTime).when(item).get("creationTime");
    Mockito.doReturn(currTime).when(item).get("updateTime");
    Mockito.doReturn(currTime).when(item).get("certExpiryTime");
    Mockito.doReturn(currTime).when(item).getLong("creationTime");
    Mockito.doReturn(currTime).when(item).getLong("updateTime");
    Mockito.doReturn(currTime).when(item).getLong("certExpiryTime");
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, false);
    when(iteratorSupport.next()).thenReturn(item);
    Mockito.doReturn(itemCollection).when(ipIndex).query(Mockito.any(QuerySpec.class));
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    dbConn.setOperationTimeout(10);
    List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByIp("10.0.0.1");
    Assert.assertNotNull(wlRecordList);
    Assert.assertEquals(wlRecordList.get(0).getInstanceId(), "1234");
    Assert.assertEquals(wlRecordList.get(0).getProvider(), "openstack");
    Assert.assertEquals(wlRecordList.get(0).getService(), "athenz.api");
    Assert.assertEquals(wlRecordList.get(0).getHostname(), "NA");
    Assert.assertEquals(wlRecordList.get(0).getUpdateTime(), new Date(currTime));
    Assert.assertEquals(wlRecordList.get(0).getCertExpiryTime(), new Date(0));
    dbConn.close();
}
Also used : QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 10 with QueryOutcome

use of com.amazonaws.services.dynamodbv2.document.QueryOutcome in project athenz by yahoo.

the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByService.

@Test
public void testGetWorkloadRecordsByService() {
    long currTime = System.currentTimeMillis();
    Mockito.doReturn("1234").when(item).getString("instanceId");
    Mockito.doReturn("openstack").when(item).getString("provider");
    Mockito.doReturn("10.10.10.11").when(item).getString("ip");
    Mockito.doReturn("test-host").when(item).getString("hostname");
    Mockito.doReturn(true).when(item).hasAttribute("hostname");
    Mockito.doReturn(currTime).when(item).get("creationTime");
    Mockito.doReturn(currTime).when(item).get("updateTime");
    Mockito.doReturn(currTime).when(item).get("certExpiryTime");
    Mockito.doReturn(currTime).when(item).getLong("creationTime");
    Mockito.doReturn(currTime).when(item).getLong("updateTime");
    Mockito.doReturn(currTime).when(item).getLong("certExpiryTime");
    Mockito.doReturn(true).when(item).hasAttribute("certExpiryTime");
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, false);
    when(iteratorSupport.next()).thenReturn(item);
    Mockito.doReturn(itemCollection).when(serviceIndex).query(Mockito.any(QuerySpec.class));
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    dbConn.setOperationTimeout(10);
    List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByService("athenz", "api");
    Assert.assertNotNull(wlRecordList);
    Assert.assertEquals(wlRecordList.get(0).getInstanceId(), "1234");
    Assert.assertEquals(wlRecordList.get(0).getProvider(), "openstack");
    Assert.assertEquals(wlRecordList.get(0).getIp(), "10.10.10.11");
    Assert.assertEquals(wlRecordList.get(0).getHostname(), "test-host");
    Assert.assertEquals(wlRecordList.get(0).getUpdateTime(), new Date(currTime));
    Assert.assertEquals(wlRecordList.get(0).getCertExpiryTime(), new Date(currTime));
    dbConn.close();
}
Also used : QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)18 Item (com.amazonaws.services.dynamodbv2.document.Item)10 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10 Table (com.amazonaws.services.dynamodbv2.document.Table)10 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)10 Test (org.testng.annotations.Test)6 Date (java.util.Date)5 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Index (com.amazonaws.services.dynamodbv2.document.Index)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2 TransactionConflictException (com.amazonaws.services.dynamodbv2.model.TransactionConflictException)2 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)2 HashMap (java.util.HashMap)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)1 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)1 ItemCollection (com.amazonaws.services.dynamodbv2.document.ItemCollection)1