public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) throws IOException { return table.checkAndPut(row, family, qualifier, value, put); }
@Override public Boolean call() throws IOException { return normalHTable.checkAndPut(row, family, qualifier, value, put); } });
@Override public void updateMaxTimestamp(long previousMaxTimestamp, long newMaxTimestamp) throws IOException { if (newMaxTimestamp < 0) { LOG.error("Negative value received for maxTimestamp: {}", newMaxTimestamp); throw new IllegalArgumentException(); } Put put = new Put(TSO_ROW); put.add(TSO_FAMILY, TSO_QUALIFIER, Bytes.toBytes(newMaxTimestamp)); byte[] previousVal = null; if (previousMaxTimestamp != INITIAL_MAX_TS_VALUE) { previousVal = Bytes.toBytes(previousMaxTimestamp); } if (!table.checkAndPut(TSO_ROW, TSO_FAMILY, TSO_QUALIFIER, previousVal, put)) { throw new IOException("Previous max timestamp is incorrect"); } }
@Override public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) throws IOException { return getTable().checkAndPut(row, family, qualifier, value, put); }
@Override public void updateMaxTimestamp(long previousMaxTimestamp, long newMaxTimestamp) throws IOException { if (newMaxTimestamp < 0) { LOG.error("Negative value received for maxTimestamp: {}", newMaxTimestamp); throw new IllegalArgumentException("Negative value received for maxTimestamp" + newMaxTimestamp); } Put put = new Put(TSO_ROW); put.add(cfName, TSO_QUALIFIER, Bytes.toBytes(newMaxTimestamp)); byte[] previousVal = null; if (previousMaxTimestamp != INITIAL_MAX_TS_VALUE) { previousVal = Bytes.toBytes(previousMaxTimestamp); } if (!table.checkAndPut(TSO_ROW, cfName, TSO_QUALIFIER, previousVal, put)) { throw new IOException("Previous max timestamp is incorrect"); } }
@Override public void updateMaxTimestamp(long previousMaxTimestamp, long newMaxTimestamp) throws IOException { if (newMaxTimestamp < 0) { LOG.error("Negative value received for maxTimestamp: {}", newMaxTimestamp); throw new IllegalArgumentException("Negative value received for maxTimestamp" + newMaxTimestamp); } Put put = new Put(TSO_ROW); put.add(cfName, TSO_QUALIFIER, Bytes.toBytes(newMaxTimestamp)); byte[] previousVal = null; if (previousMaxTimestamp != INITIAL_MAX_TS_VALUE) { previousVal = Bytes.toBytes(previousMaxTimestamp); } if (!table.checkAndPut(TSO_ROW, cfName, TSO_QUALIFIER, previousVal, put)) { throw new IOException("Previous max timestamp is incorrect"); } }
@Override public void updateMaxTimestamp(long previousMaxTimestamp, long newMaxTimestamp) throws IOException { if (newMaxTimestamp < 0) { LOG.error("Negative value received for maxTimestamp: {}", newMaxTimestamp); throw new IllegalArgumentException("Negative value received for maxTimestamp" + newMaxTimestamp); } Put put = new Put(TSO_ROW); put.add(cfName, TSO_QUALIFIER, Bytes.toBytes(newMaxTimestamp)); byte[] previousVal = null; if (previousMaxTimestamp != INITIAL_MAX_TS_VALUE) { previousVal = Bytes.toBytes(previousMaxTimestamp); } if (!table.checkAndPut(TSO_ROW, cfName, TSO_QUALIFIER, previousVal, put)) { throw new IOException("Previous max timestamp is incorrect"); } }
@Override public ListenableFuture<Boolean> tryInvalidateTransaction(long startTimestamp) { SettableFuture<Boolean> f = SettableFuture.create(); try { byte[] row = startTimestampToKey(startTimestamp); Put invalidationPut = new Put(row, startTimestamp); invalidationPut.add(commitTableFamily, INVALID_TX_QUALIFIER, null); // We need to write to the invalid column only if the commit timestamp // is empty. This has to be done atomically. Otherwise, if we first // check the commit timestamp and right before the invalidation a commit // timestamp is added and read by a transaction, then snapshot isolation // might not be hold (due to the invalidation) // TODO: Decide what we should we do if we can not contact the commit table. loop till succeed??? boolean result = table.checkAndPut(row, commitTableFamily, COMMIT_TABLE_QUALIFIER, null, invalidationPut); f.set(result); } catch (IOException ioe) { f.setException(ioe); } return f; }
@Override protected boolean claimFifoEntry(byte[] row, byte[] value, byte[] oldValue) throws IOException { Put put = new Put(keyDistributor.getDistributedKey(row)); put.add(QueueEntryRow.COLUMN_FAMILY, stateColumnName, value); return hTable.checkAndPut(put.getRow(), QueueEntryRow.COLUMN_FAMILY, stateColumnName, oldValue, put); }
@Override protected boolean claimEntry(byte[] rowKey, byte[] claimedStateValue) throws IOException { Put put = new Put(queueStrategy.getActualRowKey(getConfig(), rowKey)); put.add(QueueEntryRow.COLUMN_FAMILY, stateColumnName, claimedStateValue); return hTable.checkAndPut(put.getRow(), QueueEntryRow.COLUMN_FAMILY, stateColumnName, null, put); }
@Override public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException { byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId); boolean completed = false; try { // Keep trying to delete while (!completed) { TopicMetadata oldMetadata = getMetadata(topicId); TreeMap<String, String> newProperties = new TreeMap<>(oldMetadata.getProperties()); newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration() * -1)); Put put = tableUtil.buildPut(rowKey) .add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))) .build(); byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE)); completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put); } } catch (IOException e) { throw exceptionHandler.handle(e); } }
@Override public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException { byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId); boolean completed = false; try { // Keep trying to delete while (!completed) { TopicMetadata oldMetadata = getMetadata(topicId); TreeMap<String, String> newProperties = new TreeMap<>(oldMetadata.getProperties()); newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration() * -1)); Put put = tableUtil.buildPut(rowKey) .add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))) .build(); byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE)); completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put); } } catch (IOException e) { throw exceptionHandler.handle(e); } }
/** * * @see com.alibaba.wasp.fserver.redo.Redo#commit(com.alibaba.wasp.fserver.redo.Transaction) */ @Override public boolean commit(WALEdit edit) throws NotInitlizedRedoException, RedoLogNotServingException, AlreadyCommitTransactionException { checkInit(); Transaction t = edit.getT(); byte[] rowKey = t.getKey(); Put put = new Put(rowKey); put.add(fEntityValue, cCommit, commited); try { boolean success = transcationTable.checkAndPut(t.getKey(), fEntityValue, cCommit, unCommited, put); if (!success) { throw new AlreadyCommitTransactionException(t); } lastCommitedTransaction = edit; t.commit(t.getTransactionID()); if (LOG.isDebugEnabled()) { LOG.debug("RedoLog Commit Transaction:" + t.transactionID); } return true; } catch (IOException e) { LOG.error("Failed to commit transaction:" + t.getKeyStr(), e); return false; } }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { byte[] rowKey = MessagingUtils.toMetadataRowKey(topicMetadata.getTopicId()); boolean completed = false; try { // Keep trying to update while (!completed) { TopicMetadata oldMetadata = getMetadata(topicMetadata.getTopicId()); TreeMap<String, String> newProperties = new TreeMap<>(topicMetadata.getProperties()); newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration())); Put put = tableUtil.buildPut(rowKey) .add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))) .build(); byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE)); completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put); } } catch (IOException e) { throw exceptionHandler.handle(e); } }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { byte[] rowKey = MessagingUtils.toMetadataRowKey(topicMetadata.getTopicId()); boolean completed = false; try { // Keep trying to update while (!completed) { TopicMetadata oldMetadata = getMetadata(topicMetadata.getTopicId()); TreeMap<String, String> newProperties = new TreeMap<>(topicMetadata.getProperties()); newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration())); Put put = tableUtil.buildPut(rowKey) .add(columnFamily, COL, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE))) .build(); byte[] oldValue = Bytes.toBytes(GSON.toJson(new TreeMap<>(oldMetadata.getProperties()), MAP_TYPE)); completed = hTable.checkAndPut(rowKey, columnFamily, COL, oldValue, put); } } catch (IOException e) { throw exceptionHandler.handle(e); } }
put.add(fEntityValue, cEntityValue, t.getTransactionEntity()); put.add(fEntityValue, cCommit, unCommited); if (!transcationTable.checkAndPut(t.getKey(), fEntityValue, cEntityValue, null, put)) { throw new AlreadyExsitsTransactionIdException("TransactionId:"
properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION); putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(properties, MAP_TYPE))); completed = hTable.checkAndPut(rowKey, columnFamily, COL, null, putBuilder.build()); } else { Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE); completed = hTable.checkAndPut(rowKey, columnFamily, COL, value, putBuilder.build());
properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION); putBuilder.add(columnFamily, COL, Bytes.toBytes(GSON.toJson(properties, MAP_TYPE))); completed = hTable.checkAndPut(rowKey, columnFamily, COL, null, putBuilder.build()); } else { Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE); completed = hTable.checkAndPut(rowKey, columnFamily, COL, value, putBuilder.build());
@Override public boolean swap(byte[] row, byte[] column, byte[] oldValue, byte[] newValue) { try { byte[] distributedKey = createDistributedRowKey(row); if (newValue == null) { // HBase API weirdness: we must use deleteColumns() because deleteColumn() deletes only the last version. Delete delete = tableUtil.buildDelete(distributedKey) .deleteColumns(columnFamily, column) .build(); return hTable.checkAndDelete(distributedKey, columnFamily, column, oldValue, delete); } else { Put put = tableUtil.buildPut(distributedKey) .add(columnFamily, column, newValue) .build(); return hTable.checkAndPut(distributedKey, columnFamily, column, oldValue, put); } } catch (IOException e) { throw new DataSetException("Swap failed on table " + tableId, e); } }
@Override public boolean swap(byte[] row, byte[] column, byte[] oldValue, byte[] newValue) { try { byte[] distributedKey = createDistributedRowKey(row); if (newValue == null) { // HBase API weirdness: we must use deleteColumns() because deleteColumn() deletes only the last version. Delete delete = tableUtil.buildDelete(distributedKey) .deleteColumns(columnFamily, column) .build(); return hTable.checkAndDelete(distributedKey, columnFamily, column, oldValue, delete); } else { Put put = tableUtil.buildPut(distributedKey) .add(columnFamily, column, newValue) .build(); return hTable.checkAndPut(distributedKey, columnFamily, column, oldValue, put); } } catch (IOException e) { throw new DataSetException("Swap failed on table " + tableId, e); } }