Search in sources :

Example 1 with LedgerMetadataFormat

use of org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat in project bookkeeper by apache.

the class LedgerMetadataTest method testStoreSystemtimeAsLedgerCtimeEnabled.

@Test
public void testStoreSystemtimeAsLedgerCtimeEnabled() throws Exception {
    LedgerMetadata lm = new LedgerMetadata(3, 3, 2, DigestType.CRC32, passwd, Collections.emptyMap(), true);
    LedgerMetadataFormat format = lm.buildProtoFormat();
    assertTrue(format.hasCtime());
}
Also used : LedgerMetadataFormat(org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat) Test(org.junit.Test)

Example 2 with LedgerMetadataFormat

use of org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat in project bookkeeper by apache.

the class LedgerMetadata method parseConfig.

/**
 * Parses a given byte array and transforms into a LedgerConfig object.
 *
 * @param bytes
 *            byte array to parse
 * @param version
 *            version of the ledger metadata
 * @param msCtime
 *            metadata store creation time, used for legacy ledgers
 * @return LedgerConfig
 * @throws IOException
 *             if the given byte[] cannot be parsed
 */
public static LedgerMetadata parseConfig(byte[] bytes, Version version, Optional<Long> msCtime) throws IOException {
    LedgerMetadata lc = new LedgerMetadata();
    lc.version = version;
    String config = new String(bytes, UTF_8);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Parsing Config: {}", config);
    }
    BufferedReader reader = new BufferedReader(new StringReader(config));
    String versionLine = reader.readLine();
    if (versionLine == null) {
        throw new IOException("Invalid metadata. Content missing");
    }
    if (versionLine.startsWith(VERSION_KEY)) {
        String[] parts = versionLine.split(tSplitter);
        lc.metadataFormatVersion = Integer.parseInt(parts[1]);
    } else {
        // if no version is set, take it to be version 1
        // as the parsing is the same as what we had before
        // we introduce versions
        lc.metadataFormatVersion = 1;
        // reset the reader
        reader.close();
        reader = new BufferedReader(new StringReader(config));
    }
    if (lc.metadataFormatVersion < LOWEST_COMPAT_METADATA_FORMAT_VERSION || lc.metadataFormatVersion > CURRENT_METADATA_FORMAT_VERSION) {
        throw new IOException("Metadata version not compatible. Expected between " + LOWEST_COMPAT_METADATA_FORMAT_VERSION + " and " + CURRENT_METADATA_FORMAT_VERSION + ", but got " + lc.metadataFormatVersion);
    }
    if (lc.metadataFormatVersion == 1) {
        return parseVersion1Config(lc, reader);
    }
    // remaining size is total minus the length of the version line and '\n'
    char[] configBuffer = new char[config.length() - (versionLine.length() + 1)];
    if (configBuffer.length != reader.read(configBuffer, 0, configBuffer.length)) {
        throw new IOException("Invalid metadata buffer");
    }
    LedgerMetadataFormat.Builder builder = LedgerMetadataFormat.newBuilder();
    TextFormat.merge((CharSequence) CharBuffer.wrap(configBuffer), builder);
    LedgerMetadataFormat data = builder.build();
    lc.writeQuorumSize = data.getQuorumSize();
    if (data.hasCtime()) {
        lc.ctime = data.getCtime();
        lc.storeSystemtimeAsLedgerCreationTime = true;
    } else if (msCtime.isPresent()) {
        lc.ctime = msCtime.get();
        lc.storeSystemtimeAsLedgerCreationTime = false;
    }
    if (data.hasAckQuorumSize()) {
        lc.ackQuorumSize = data.getAckQuorumSize();
    } else {
        lc.ackQuorumSize = lc.writeQuorumSize;
    }
    lc.ensembleSize = data.getEnsembleSize();
    lc.length = data.getLength();
    lc.state = data.getState();
    lc.lastEntryId = data.getLastEntryId();
    if (data.hasPassword()) {
        lc.digestType = data.getDigestType();
        lc.password = data.getPassword().toByteArray();
        lc.hasPassword = true;
    }
    for (LedgerMetadataFormat.Segment s : data.getSegmentList()) {
        ArrayList<BookieSocketAddress> addrs = new ArrayList<BookieSocketAddress>();
        for (String member : s.getEnsembleMemberList()) {
            addrs.add(new BookieSocketAddress(member));
        }
        lc.addEnsemble(s.getFirstEntryId(), addrs);
    }
    if (data.getCustomMetadataCount() > 0) {
        List<LedgerMetadataFormat.cMetadataMapEntry> cMetadataList = data.getCustomMetadataList();
        lc.customMetadata = Maps.newHashMap();
        for (LedgerMetadataFormat.cMetadataMapEntry ent : cMetadataList) {
            lc.customMetadata.put(ent.getKey(), ent.getValue().toByteArray());
        }
    }
    return lc;
}
Also used : ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) LedgerMetadataFormat(org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat)

Example 3 with LedgerMetadataFormat

use of org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat in project bookkeeper by apache.

the class LedgerMetadata method buildProtoFormat.

LedgerMetadataFormat buildProtoFormat(boolean withPassword) {
    LedgerMetadataFormat.Builder builder = LedgerMetadataFormat.newBuilder();
    builder.setQuorumSize(writeQuorumSize).setAckQuorumSize(ackQuorumSize).setEnsembleSize(ensembleSize).setLength(length).setState(state).setLastEntryId(lastEntryId);
    if (storeSystemtimeAsLedgerCreationTime) {
        builder.setCtime(ctime);
    }
    if (hasPassword) {
        builder.setDigestType(digestType);
        if (withPassword) {
            builder.setPassword(ByteString.copyFrom(password));
        }
    }
    if (customMetadata != null) {
        LedgerMetadataFormat.cMetadataMapEntry.Builder cMetadataBuilder = LedgerMetadataFormat.cMetadataMapEntry.newBuilder();
        for (Map.Entry<String, byte[]> entry : customMetadata.entrySet()) {
            cMetadataBuilder.setKey(entry.getKey()).setValue(ByteString.copyFrom(entry.getValue()));
            builder.addCustomMetadata(cMetadataBuilder.build());
        }
    }
    for (Map.Entry<Long, ArrayList<BookieSocketAddress>> entry : ensembles.entrySet()) {
        LedgerMetadataFormat.Segment.Builder segmentBuilder = LedgerMetadataFormat.Segment.newBuilder();
        segmentBuilder.setFirstEntryId(entry.getKey());
        for (BookieSocketAddress addr : entry.getValue()) {
            segmentBuilder.addEnsembleMember(addr.toString());
        }
        builder.addSegment(segmentBuilder.build());
    }
    return builder.build();
}
Also used : ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) LedgerMetadataFormat(org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat)

Example 4 with LedgerMetadataFormat

use of org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat in project bookkeeper by apache.

the class LedgerMetadataTest method testStoreSystemtimeAsLedgerCtimeDisabled.

@Test
public void testStoreSystemtimeAsLedgerCtimeDisabled() throws Exception {
    LedgerMetadata lm = new LedgerMetadata(3, 3, 2, DigestType.CRC32, passwd, Collections.emptyMap(), false);
    LedgerMetadataFormat format = lm.buildProtoFormat();
    assertFalse(format.hasCtime());
}
Also used : LedgerMetadataFormat(org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat) Test(org.junit.Test)

Aggregations

LedgerMetadataFormat (org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat)4 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1