@Override public Health getHealth() { try { ColumnFamily<String, String> CF_SYSTEM_LOCAL = new ColumnFamily<String, String>( "system.local", StringSerializer.get(), StringSerializer.get(), StringSerializer.get() ); OperationResult<CqlResult<String, String>> result = keyspace.prepareQuery( CF_SYSTEM_LOCAL ) .setConsistencyLevel(ConsistencyLevel.CL_ONE) .withCql( "SELECT now() FROM system.local;" ) .execute(); if ( result.getResult().getRows().size() > 0 ) { return Health.GREEN; } } catch ( ConnectionException ex ) { logger.error( "Error connecting to Cassandra", ex ); } return Health.RED; } }
latestEntityColumns = keyspace.prepareQuery( CF_ENTITY_LOG ).getKeySlice( rowKeys ) .withColumnRange( maxVersion, null, false, 1 ).execute().getResult() .iterator();
/** * Return true if we have < 2 rows with columns, false otherwise */ private boolean containsSingleRowOnly( final Rows<R, C> result ) { int count = 0; for ( R key : result.getKeys() ) { if ( result.getRow( key ).getColumns().size() > 0 ) { count++; //we have more than 1 row with values, return them if ( count > 1 ) { return false; } } } return true; }
when(response.getRowByIndex(0)).thenReturn(row0); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1);
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsRawIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ String list = dao.getFilterIdsRaw("index"); /* validate responses */ assertEquals(fids, list); }
private static void mockGetScriptForEndpoint(Rows<String, String> response, String filter) { /* mock data so that the getScriptForEndpoint at the end of the method will work */ Calendar now = Calendar.getInstance(); Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_id", filter); mockColumn(columnList0, "revision", 1L); mockColumn(columnList0, "active", false); mockColumn(columnList0, "creation_date", now.getTime()); mockColumn(columnList0, "filter_code", "System.out.println(\"hello world\")".getBytes()); // what we put here doesn't matter mockColumn(columnList0, "filter_name", "name"); mockColumn(columnList0, "filter_type", "INBOUND"); mockColumn(columnList0, "canary", false); when(response.getRowByIndex(0)).thenReturn(row0); when(response.isEmpty()).thenReturn(true, false); // true on the first request, false thereafter when(response.size()).thenReturn(1); } }
@Override public boolean hasRows() { return rows != null && !this.rows.isEmpty(); }
private void testRowKeysWithAllColumns(boolean rowDeleted) throws Exception { Set<String> rowKeys = getRandomRowKeys(); Rows<String, String> rows = keyspace.prepareQuery(CF_COLUMN_RANGE_TEST).getRowSlice(rowKeys).execute().getResult(); if (rowDeleted) { Assert.assertTrue(rows.isEmpty()); return; } Assert.assertFalse(rows.isEmpty()); int rowKeysSize = rowKeys.size(); for (Row<String, String> row : rows) { boolean isPresent = rowKeys.remove(row.getKey()); Assert.assertTrue("Extraneous row: " + row.getKey(), isPresent); ColumnList<String> colList = row.getColumns(); Assert.assertEquals(26, colList.size()); for(int index=0; index<26; index++) { Column<String> col = colList.getColumnByIndex(index); Assert.assertTrue(String.valueOf((char)('a' + index)).equals(col.getName())); Assert.assertEquals(index + 1, col.getIntegerValue()); } } Assert.assertEquals(rowKeysSize, rows.size()); }
@Test public void testGetAllKeysRoot() throws ConnectionException { LOG.info("Starting testGetAllKeysRoot..."); List<String> keys = new ArrayList<String>(); for (char key = 'A'; key <= 'Z'; key++) { String keyName = Character.toString(key); keys.add(keyName); } OperationResult<Rows<String, String>> result = keyspace .prepareQuery(CF_STANDARD1) .getKeySlice(keys.toArray(new String[keys.size()])) .execute(); Assert.assertEquals(26, result.getResult().size()); Row<String, String> row; row = result.getResult().getRow("A"); Assert.assertEquals("A", row.getKey()); row = result.getResult().getRow("B"); Assert.assertEquals("B", row.getKey()); row = result.getResult().getRow("NonExistent"); Assert.assertNull(row); row = result.getResult().getRowByIndex(10); Assert.assertEquals("K", row.getKey()); LOG.info("... testGetAllKeysRoot"); }
public List<FilterInfo> getFiltersForIndex(String index) { List<String> filterInfoList = getFilterIdsIndex(index); if (filterInfoList.isEmpty()) { return Collections.emptyList(); } Rows<String, String> result = cassandraGateway.getByFilterIds(filterInfoList); if (result == null || result.isEmpty()) { return Collections.emptyList(); } else { List<FilterInfo> filterInfos = new ArrayList<FilterInfo>(); Iterator<Row<String, String>> rows = result.iterator(); while (rows.hasNext()) { Row<String, String> row = rows.next(); FilterInfo script = getFilterScriptFromCassandraRow(row); if (script != null) { filterInfos.add(script); } } Collections.sort(filterInfos); return filterInfos; } }
public String findKey(String app, String id, String location, String datacenter) { try { final String selectClause = String.format( "SELECT * FROM %s USING CONSISTENCY LOCAL_QUORUM WHERE %s = '%s' and %s = '%s' and %s = '%s' and %s = '%s' ", "tokens", CN_APPID, app, CN_ID, id, CN_LOCATION, location, CN_DC, datacenter); logger.info(selectClause); final ColumnFamily<String, String> CF_INSTANCES_NEW = ColumnFamily.newColumnFamily(KS_NAME, StringSerializer.get(), StringSerializer.get()); OperationResult<CqlResult<String, String>> result = bootKeyspace.prepareQuery(CF_INSTANCES_NEW) .withCql(selectClause).execute(); if (result == null || result.getResult().getRows().size() == 0) return null; Row<String, String> row = result.getResult().getRows().getRowByIndex(0); return row.getKey(); } catch (Exception e) { logger.warn("Caught an Unknown Exception during find a row matching cluster[" + app + "], id[" + id + "], and region[" + datacenter + "] ... -> " + e.getMessage()); throw new RuntimeException(e); } }
Assert.assertTrue(result.getResult().hasRows()); Assert.assertEquals(10, result.getResult().getRows().size()); Assert.assertFalse(result.getResult().hasNumber()); Row<Integer, String> row = result.getResult().getRows().getRow(i); Assert.assertTrue(i == row.getKey()); Assert.assertEquals(3, row.getColumns().size());
when(response.getRowByIndex(0)).thenReturn(row0); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1);
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsRawIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ String list = dao.getFilterIdsRaw("index"); /* validate responses */ assertEquals(fids, list); }
private static void mockGetScriptForEndpoint(Rows<String, String> response, String filter) { /* mock data so that the getScriptForEndpoint at the end of the method will work */ Calendar now = Calendar.getInstance(); Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_id", filter); mockColumn(columnList0, "revision", 1L); mockColumn(columnList0, "active", false); mockColumn(columnList0, "creation_date", now.getTime()); mockColumn(columnList0, "filter_code", "System.out.println(\"hello world\")".getBytes()); // what we put here doesn't matter mockColumn(columnList0, "filter_name", "name"); mockColumn(columnList0, "filter_type", "type"); mockColumn(columnList0, "canary", false); when(response.getRowByIndex(0)).thenReturn(row0); when(response.isEmpty()).thenReturn(true, false); // true on the first request, false thereafter when(response.size()).thenReturn(1); } }
@Override public boolean hasRows() { return rows != null && !this.rows.isEmpty(); }
public List<FilterInfo> getFiltersForIndex(String index) { List<String> filterInfoList = getFilterIdsIndex(index); if (filterInfoList.isEmpty()) { return Collections.emptyList(); } Rows<String, String> result = cassandraGateway.getByFilterIds(filterInfoList); if (result == null || result.isEmpty()) { return Collections.emptyList(); } else { List<FilterInfo> filterInfos = new ArrayList<FilterInfo>(); Iterator<Row<String, String>> rows = result.iterator(); while (rows.hasNext()) { Row<String, String> row = rows.next(); FilterInfo script = getFilterScriptFromCassandraRow(row); if (script != null) { filterInfos.add(script); } } Collections.sort(filterInfos); return filterInfos; } }