athanatos commented on a change in pull request #890: ISSUE #877: add verifier.BookkeeperVerifier URL: https://github.com/apache/bookkeeper/pull/890#discussion_r158403739
########## File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/verifier/BookkeeperVerifier.java ########## @@ -0,0 +1,692 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bookkeeper.verifier; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Random; +import java.util.Set; +import java.util.TreeSet; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +import org.apache.bookkeeper.client.BKException; + +/** + * Encapsulates logic for playing and verifying operations against a bookkeeper-like + * interface. The test state consists of a set of ledgers in 1 of several states: + * 1) opening -- waiting for driver to complete open + * 2) open -- valid targets for reads and writes + * 3) live -- valid targets for reads + * 4) deleting + * Each ledger moves in sequence through these states. See startWrite for the + * code driving the lifecycle. + */ +public class BookkeeperVerifier { + private Queue<Exception> errors = new LinkedList<>(); + + private synchronized boolean checkReturn(long ledgerID, Integer rc) { + if (rc != 0) { + String error = String.format("Got error %d on ledger %d", rc, ledgerID); + System.out.println(error); + propagateExceptionToMain(BKException.create(rc)); + return true; + } else { + return false; + } + } + + private synchronized void propagateExceptionToMain(Exception e) { + errors.add(e); + this.notifyAll(); + } + + private synchronized void printThrowExceptions() throws Exception { + if (!errors.isEmpty()) { + for (Exception e: errors) { + System.out.format("Error found: %s%n", e.toString()); + e.printStackTrace(); + } + throw errors.poll(); + } + } + + /** + * Provides an interface for translating test operations into operations on a + * cluster. + */ + public interface BookkeeperDriver { + void createLedger( + long ledgerID, int enSize, int writeQSize, int ackQSize, + Consumer<Integer> cb + ); + + void closeLedger( + long ledgerID, + Consumer<Integer> cb + ); + + void deleteLedger( + long ledgerID, + Consumer<Integer> cb + ); + + void writeEntry( + long ledgerID, + long entryID, + byte[] data, + Consumer<Integer> cb + ); + + /** + * Callback for reads. + */ + interface ReadCallback { + void complete( + long ledgerID, + ArrayList<byte[]> results + ); + } + + void readEntries( + long ledgerID, + long firstEntryID, + long lastEntryID, + BiConsumer<Integer, ArrayList<byte[]>> cb); + } + + private final BookkeeperDriver driver; + + private final int ensembleSize; + private final int writeQuorum; + private final int ackQuorum; + private final int duration; + private final int drainTimeout; + private final int targetConcurrentLedgers; + private final int targetConcurrentWrites; + private final int targetWriteGroup; + private final int targetReadGroup; + private final int targetLedgers; + private final int targetEntrySize; + private final int targetConcurrentReads; + private final double coldToHotRatio; + + private final long targetLedgerEntries; + + BookkeeperVerifier( + BookkeeperDriver driver, + int ensembleSize, + int writeQuorum, + int ackQuorum, + int duration, + int drainTimeout, + int targetConcurrentLedgers, + int targetConcurrentWrites, + int targetWriteGroup, + int targetReadGroup, + int targetLedgers, + long targetLedgerSize, + int targetEntrySize, + int targetConcurrentReads, + double coldToHotRatio) { + this.driver = driver; + this.ensembleSize = ensembleSize; + this.writeQuorum = writeQuorum; + this.ackQuorum = ackQuorum; + this.duration = duration; + this.drainTimeout = drainTimeout; + this.targetConcurrentLedgers = targetConcurrentLedgers; + this.targetConcurrentWrites = targetConcurrentWrites; + this.targetWriteGroup = targetWriteGroup; + this.targetReadGroup = targetReadGroup; + this.targetLedgers = targetLedgers; + this.targetEntrySize = targetEntrySize; + this.targetConcurrentReads = targetConcurrentReads; + this.coldToHotRatio = coldToHotRatio; + + this.targetLedgerEntries = targetLedgerSize / targetEntrySize; + } + + private int outstandingWriteCount = 0; + private int outstandingReadCount = 0; + private long nextLedger = 0; + private long getNextLedgerID() { + return nextLedger++; + } + + /** + * State required to regenerate an entry. + */ + class EntryInfo { + private long entryID; + private long seed; + EntryInfo(long entryID, long seed) { + this.entryID = entryID; + this.seed = seed; + } + byte[] getBuffer() { + Random rand = new Random(seed); + byte[] ret = new byte[targetEntrySize]; + rand.nextBytes(ret); + return ret; + } + long getEntryID() { + return entryID; + } + } + + /** + * Contains the state required to reconstruct the contents of any entry in the ledger. + * The seed value passed into the constructor fully determines the contents of the + * ledger. Each EntryInfo has its own seed generated sequentially from a Random instance + * seeded from the original seed. It then uses that seed to generate a secondary Random + * instance for generating the bytes within the entry. See EntryIterator for details. + * Random(seed) + * | + * E0 -> Random(E0) -> getBuffer() + * | + * E1 -> Random(E1) -> getBuffer() + * | + * E2 -> Random(E2) -> getBuffer() + * | + * E3 -> Random(E3) -> getBuffer() + * | + * E4 -> Random(E4) -> getBuffer() + * | + * ... + */ + class LedgerInfo { Review comment: It allocates EntryInfo's, easier to leave it non-static. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
