/** * Loads the metadata for the current log, as stored in ZooKeeper. * * @return A new LogMetadata object with the desired information, or null if no such node exists. * @throws DataLogInitializationException If an Exception (other than NoNodeException) occurred. */ @VisibleForTesting LogMetadata loadMetadata() throws DataLogInitializationException { try { Stat storingStatIn = new Stat(); byte[] serializedMetadata = this.zkClient.getData().storingStatIn(storingStatIn).forPath(this.logNodePath); LogMetadata result = LogMetadata.SERIALIZER.deserialize(serializedMetadata); result.withUpdateVersion(storingStatIn.getVersion()); return result; } catch (KeeperException.NoNodeException nne) { // Node does not exist: this is the first time we are accessing this log. log.warn("{}: No ZNode found for path '{}{}'. This is OK if this is the first time accessing this log.", this.traceObjectId, this.zkClient.getNamespace(), this.logNodePath); return null; } catch (Exception ex) { throw new DataLogInitializationException(String.format("Unable to load ZNode contents for path '%s%s'.", this.zkClient.getNamespace(), this.logNodePath), ex); } }
.forPath(this.logNodePath, serializedMetadata); metadata.withUpdateVersion(0); } else { this.zkClient.setData() metadata.withUpdateVersion(metadata.getUpdateVersion() + 1);
expectedLedgerIds.add(ledgerId); if (metadata == null) { metadata = new LogMetadata(ledgerId).withUpdateVersion(i); } else { metadata.withUpdateVersion(i); metadata = metadata.addLedger(ledgerId);
/** * Tests serialization/deserialization. */ @Test(timeout = 5000) public void testSerialization() throws Exception { Supplier<Long> nextLedgerId = new AtomicLong()::incrementAndGet; LogMetadata m1 = null; val lacs = new HashMap<Long, Long>(); for (int i = 0; i < LEDGER_COUNT; i++) { long ledgerId = nextLedgerId.get() * 2; if (m1 == null) { m1 = new LogMetadata(ledgerId).withUpdateVersion(i); } else { m1 = m1.addLedger(ledgerId).withUpdateVersion(i); } if (i % 2 == 0) { // Every other Ledger, update the LastAddConfirmed. lacs.put((long) i, (long) i + 1); } } m1 = m1.updateLedgerStatus(lacs); val serialization = LogMetadata.SERIALIZER.serialize(m1); val m2 = LogMetadata.SERIALIZER.deserialize(serialization); Assert.assertEquals("Unexpected epoch.", m1.getEpoch(), m2.getEpoch()); Assert.assertEquals("Unexpected TruncationAddress.", m1.getTruncationAddress().getSequence(), m2.getTruncationAddress().getSequence()); Assert.assertEquals("Unexpected TruncationAddress.", m1.getTruncationAddress().getLedgerId(), m2.getTruncationAddress().getLedgerId()); AssertExtensions.assertListEquals("Unexpected ledgers.", m1.getLedgers(), m2.getLedgers(), (l1, l2) -> l1.getSequence() == l2.getSequence() && l1.getLedgerId() == l2.getLedgerId() && l1.getStatus() == l2.getStatus()); }
long ledgerId = nextLedgerId.get() * 2; if (m == null) { m = new LogMetadata(ledgerId).withUpdateVersion(i); } else { m.withUpdateVersion(i); m = m.addLedger(ledgerId);