public synchronized <T extends MetricsMetaKey> void save(Map<T, Long> offsets) throws Exception { SortedMap<byte[], SortedMap<byte[], Long>> updates = new TreeMap<>(Bytes.BYTES_COMPARATOR); for (Map.Entry<T, Long> entry : offsets.entrySet()) { SortedMap<byte[], Long> map = new TreeMap<>(Bytes.BYTES_COMPARATOR); map.put(OFFSET_COLUMN, entry.getValue()); updates.put(entry.getKey().getKey(), map); } metaTable.put(updates); }
@Override public void put(SortedMap<byte[], ? extends SortedMap<byte[], Long>> updates) { deleteColumns(updates); v3HBaseTable.put(updates); }
@Override public void apply() { delegate.put(updates); } });
public synchronized <T extends MetricsMetaKey> void save(Map<T, Long> offsets) throws Exception { SortedMap<byte[], SortedMap<byte[], Long>> updates = new TreeMap<>(Bytes.BYTES_COMPARATOR); for (Map.Entry<T, Long> entry : offsets.entrySet()) { SortedMap<byte[], Long> map = new TreeMap<>(Bytes.BYTES_COMPARATOR); map.put(OFFSET_COLUMN, entry.getValue()); updates.put(entry.getKey().getKey(), map); } metaTable.put(updates); }
public void add(List<Fact> facts) { // Simply collecting all rows/cols/values that need to be put to the underlying table. NavigableMap<byte[], NavigableMap<byte[], byte[]>> gaugesTable = Maps.newTreeMap(Bytes.BYTES_COMPARATOR); NavigableMap<byte[], NavigableMap<byte[], byte[]>> incrementsTable = Maps.newTreeMap(Bytes.BYTES_COMPARATOR); for (Fact fact : facts) { for (Measurement measurement : fact.getMeasurements()) { byte[] rowKey = codec.createRowKey(fact.getDimensionValues(), measurement.getName(), fact.getTimestamp()); byte[] column = codec.createColumn(fact.getTimestamp()); if (MeasureType.COUNTER == measurement.getType()) { inc(incrementsTable, rowKey, column, measurement.getValue()); } else { set(gaugesTable, rowKey, column, Bytes.toBytes(measurement.getValue())); } } } NavigableMap<byte[], NavigableMap<byte[], Long>> convertedIncrementsTable = Maps.transformValues(incrementsTable, TRANSFORM_MAP_BYTE_ARRAY_TO_LONG); NavigableMap<byte[], NavigableMap<byte[], Long>> convertedGaugesTable = Maps.transformValues(gaugesTable, TRANSFORM_MAP_BYTE_ARRAY_TO_LONG); // todo: replace with single call, to be able to optimize rpcs in underlying table timeSeriesTable.put(convertedGaugesTable); timeSeriesTable.increment(convertedIncrementsTable); if (metrics != null) { metrics.increment(putCountMetric, convertedGaugesTable.size()); metrics.increment(incrementCountMetric, convertedIncrementsTable.size()); } }
@Test public void testConcurrentSwap() throws Exception { MetricsTable table = getTable("testConcurrentSwap"); final long rounds = 500; table.put(ImmutableSortedMap.<byte[], SortedMap<byte[], Long>>orderedBy(Bytes.BYTES_COMPARATOR) .put(A, mapOf(B, 0L)).build()); AtomicInteger[] counts = { new AtomicInteger(), new AtomicInteger() }; // [0] for success, [1] for failures Thread t1 = new SwapThread(table, A, B, counts, rounds); Thread t2 = new SwapThread(table, A, B, counts, rounds); t1.start(); t2.start(); t1.join(); t2.join(); Assert.assertEquals(rounds, Bytes.toLong(table.get(A, B))); Assert.assertEquals(rounds, counts[0].get()); // number of successful swaps Assert.assertEquals(rounds, counts[1].get()); // number of failed swaps }
timeSeriesTable.put(gaugesTable); timeSeriesTable.increment(incrementsTable); if (metrics != null) {
table.put(writes);
@Test public void testDelete() throws Exception { MetricsTable table = getTable("testDelete"); NavigableMap<byte[], SortedMap<byte[], Long>> writes = Maps.newTreeMap(Bytes.BYTES_COMPARATOR); for (int i = 0; i < 1024; i++) { writes.put(Bytes.toBytes(i << 22), mapOf(A, Bytes.toLong(X))); } table.put(writes); // verify the first and last are there (sanity test for correctness of test logic) Assert.assertArrayEquals(X, table.get(Bytes.toBytes(0x00000000), A)); Assert.assertArrayEquals(X, table.get(Bytes.toBytes(0xffc00000), A)); List<byte[]> toDelete = ImmutableList.of( Bytes.toBytes(0xa1000000), Bytes.toBytes(0xb2000000), Bytes.toBytes(0xc3000000)); // verify these three are there, and delete them for (byte[] row : toDelete) { Assert.assertArrayEquals(X, table.get(row, A)); table.delete(row, new byte[][] {A}); } // verify these three are now gone. for (byte[] row : toDelete) { Assert.assertNull(table.get(row, A)); } // verify nothing else is gone by counting all entries in a scan Assert.assertEquals(1021, countRange(table, null, null)); }
MetricsTable table = getTable("testGetPutSwap"); table.put(ImmutableSortedMap.<byte[], SortedMap<byte[], Long>>orderedBy(Bytes.BYTES_COMPARATOR) .put(A, mapOf(P, Bytes.toLong(X), Q, Bytes.toLong(Y))) .put(B, mapOf(P, Bytes.toLong(X), R, Bytes.toLong(Z))).build()); table.put(ImmutableSortedMap.<byte[], SortedMap<byte[], Long>>orderedBy(Bytes.BYTES_COMPARATOR) .put(A, mapOf(P, Bytes.toLong(A), R, Bytes.toLong(C))).build()); Assert.assertEquals(Bytes.toLong(A), Bytes.toLong(table.get(A, P)));