@Override public int getVersion() { return getCurrentCodec().getVersion(); }
@Override public void writeSnapshot(OutputStream out, TransactionSnapshot snapshot) throws IOException { codecProvider.encode(out, snapshot); } }
private TransactionManagerDebuggerMain (Configuration configuration) { codecProvider = new SnapshotCodecProvider(configuration); buildOptions(); this.hConf = configuration; }
@Override public void encode(OutputStream out, TransactionSnapshot snapshot) { SnapshotCodec codec = getCurrentCodec(); try { new BinaryEncoder(out).writeInt(codec.getVersion()); } catch (IOException e) { LOG.error("Unable to write transaction state version: ", e); throw Throwables.propagate(e); } codec.encode(out, snapshot); }
@Override public void encode(OutputStream out, TransactionSnapshot snapshot) { try { BinaryEncoder encoder = new BinaryEncoder(out); encoder.writeLong(snapshot.getTimestamp()); encoder.writeLong(snapshot.getReadPointer()); encoder.writeLong(snapshot.getWritePointer()); encodeInvalid(encoder, snapshot.getInvalid()); encodeInProgress(encoder, snapshot.getInProgress()); encodeChangeSets(encoder, snapshot.getCommittingChangeSets()); encodeChangeSets(encoder, snapshot.getCommittedChangeSets()); } catch (IOException e) { LOG.error("Unable to serialize transaction state: ", e); throw Throwables.propagate(e); } }
@Override public TransactionVisibilityState decodeTransactionVisibilityState(InputStream in) { BinaryDecoder decoder = new BinaryDecoder(in); try { long timestamp = decoder.readLong(); long readPointer = decoder.readLong(); long writePointer = decoder.readLong(); Collection<Long> invalid = decodeInvalid(decoder); NavigableMap<Long, TransactionManager.InProgressTx> inProgress = decodeInProgress(decoder); return new TransactionSnapshot(timestamp, readPointer, writePointer, invalid, inProgress); } catch (IOException e) { LOG.error("Unable to deserialize transaction state: ", e); throw Throwables.propagate(e); } }
private NavigableMap<Long, Set<ChangeId>> decodeChangeSets(BinaryDecoder decoder) throws IOException { int size = decoder.readInt(); NavigableMap<Long, Set<ChangeId>> changeSets = new TreeMap<Long, Set<ChangeId>>(); while (size != 0) { // zero denotes end of list as per AVRO spec for (int remaining = size; remaining > 0; --remaining) { changeSets.put(decoder.readLong(), decodeChanges(decoder)); } size = decoder.readInt(); } return changeSets; }
private SnapshotCodec getCodec(InputStream in) { BinaryDecoder decoder = new BinaryDecoder(in); int persistedVersion; try { persistedVersion = decoder.readInt(); } catch (IOException e) { LOG.error("Unable to read transaction state version: ", e); throw Throwables.propagate(e); } return getCodecForVersion(persistedVersion); }
private void encodeChangeSets(BinaryEncoder encoder, Map<Long, Set<ChangeId>> changes) throws IOException { if (!changes.isEmpty()) { encoder.writeInt(changes.size()); for (Map.Entry<Long, Set<ChangeId>> entry : changes.entrySet()) { encoder.writeLong(entry.getKey()); encodeChanges(encoder, entry.getValue()); } } encoder.writeInt(0); // zero denotes end of list as per AVRO spec }
@Override public TransactionVisibilityState decodeTransactionVisibilityState(InputStream in) { return getCodec(in).decodeTransactionVisibilityState(in); }
private TransactionSnapshot readSnapshotFile(InputStream is) throws IOException { return codecProvider.decode(is); }
/** * Read one int from the input. * @return the read number * @throws java.io.IOException If there is IO error. * @throws java.io.EOFException If end of file reached. */ public int readInt() throws IOException { int val = 0; int shift = 0; int b = readByte(); while (b > 0x7f) { val ^= (b & 0x7f) << shift; shift += 7; b = readByte(); } val ^= b << shift; return (val >>> 1) ^ -(val & 1); }
@Override public void writeSnapshot(OutputStream out, TransactionSnapshot snapshot) throws IOException { codec.encode(out, snapshot); }
@Inject public SnapshotCodecProvider(Configuration configuration) { initialize(configuration); }
/** * write a sequence of bytes. First writes the number of bytes as an int, then the bytes themselves. * @throws java.io.IOException If there is IO error. */ public BinaryEncoder writeBytes(byte[] bytes) throws IOException { writeLong(bytes.length); output.write(bytes, 0, bytes.length); return this; } }
private TransactionManagerDebuggerMain (Configuration configuration) { codecProvider = new SnapshotCodecProvider(configuration); buildOptions(); this.hConf = configuration; }
/** * Read one long int from the input. * @return the read number * @throws java.io.IOException If there is IO error. * @throws java.io.EOFException If end of file reached. */ public long readLong() throws IOException { long val = 0; int shift = 0; int b = readByte(); while (b > 0x7f) { val ^= (long) (b & 0x7f) << shift; shift += 7; b = readByte(); } val ^= (long) b << shift; return (val >>> 1) ^ -(val & 1); }
@Override public PhoenixTransactionService getTransactionService(Configuration config, ConnectionInfo connInfo, int port) { config.setInt(TxConstants.Service.CFG_DATA_TX_BIND_PORT, port); int retryTimeOut = config.getInt(TxConstants.Service.CFG_DATA_TX_CLIENT_DISCOVERY_TIMEOUT_SEC, TxConstants.Service.DEFAULT_DATA_TX_CLIENT_DISCOVERY_TIMEOUT_SEC); ZKClientService zkClient = ZKClientServices.delegate( ZKClients.reWatchOnExpire( ZKClients.retryOnFailure( ZKClientService.Builder.of(connInfo.getZookeeperConnectionString()) .setSessionTimeout(config.getInt(HConstants.ZK_SESSION_TIMEOUT, HConstants.DEFAULT_ZK_SESSION_TIMEOUT)) .build(), RetryStrategies.exponentialDelay(500, retryTimeOut, TimeUnit.MILLISECONDS) ) ) ); DiscoveryService discovery = new ZKDiscoveryService(zkClient); TransactionManager txManager = new TransactionManager(config, new HDFSTransactionStateStorage(config, new SnapshotCodecProvider(config), new TxMetricsCollector()), new TxMetricsCollector()); TransactionService txService = new TransactionService(config, zkClient, discovery, Providers.of(txManager)); TephraTransactionService service = new TephraTransactionService(zkClient, txService); service.start(); return service; }