Skip to content

Commit

Permalink
Use mockito instead of powermock (#4212)
Browse files Browse the repository at this point in the history
  • Loading branch information
nodece authored Feb 20, 2024
1 parent ef4ce13 commit ee56a95
Show file tree
Hide file tree
Showing 47 changed files with 639 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public BookieImpl(ServerConfiguration conf,
// instantiate the journals
journals = Lists.newArrayList();
for (int i = 0; i < journalDirectories.size(); i++) {
journals.add(new Journal(i, journalDirectories.get(i),
journals.add(Journal.newJournal(i, journalDirectories.get(i),
conf, ledgerDirsManager, statsLogger.scope(JOURNAL_SCOPE), allocator, journalAliveListener));
}

Expand Down Expand Up @@ -496,6 +496,21 @@ public void ledgerDeleted(long ledgerId) {
this.bookieStats = new BookieStats(statsLogger, journalDirectories.size(), conf.getJournalQueueSize());
}

@VisibleForTesting
public static BookieImpl newBookieImpl(ServerConfiguration conf,
RegistrationManager registrationManager,
LedgerStorage storage,
DiskChecker diskChecker,
LedgerDirsManager ledgerDirsManager,
LedgerDirsManager indexDirsManager,
StatsLogger statsLogger,
ByteBufAllocator allocator,
Supplier<BookieServiceInfo> bookieServiceInfoProvider)
throws IOException, InterruptedException, BookieException {
return new BookieImpl(conf, registrationManager, storage, diskChecker,
ledgerDirsManager, indexDirsManager, statsLogger, allocator, bookieServiceInfoProvider);
}

StateManager initializeStateManager() throws IOException {
return new BookieStateManager(conf, statsLogger, registrationManager,
ledgerDirsManager, bookieServiceInfoProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
package org.apache.bookkeeper.bookie;

import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithLedgerManagerFactory;
import static org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand.newLastMarkCommand;
import static org.apache.bookkeeper.tools.cli.commands.bookies.ClusterInfoCommand.newClusterInfoCommand;
import static org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand.newListBookiesCommand;
import static org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand.newSimpleTestCommand;

import com.google.common.annotations.VisibleForTesting;
import java.io.File;
Expand Down Expand Up @@ -1007,13 +1011,13 @@ public int runCmd(CommandLine cmdLine) throws Exception {
int ackQuorum = getOptionIntValue(cmdLine, "ackQuorum", 2);
int numEntries = getOptionIntValue(cmdLine, "numEntries", 1000);

SimpleTestCommand.Flags flags = new SimpleTestCommand.Flags()
SimpleTestCommand.Flags flags = SimpleTestCommand.Flags.newFlags()
.ensembleSize(ensemble)
.writeQuorumSize(writeQuorum)
.ackQuorumSize(ackQuorum)
.numEntries(numEntries);

SimpleTestCommand command = new SimpleTestCommand(flags);
SimpleTestCommand command = newSimpleTestCommand(flags);

command.apply(bkConf, flags);
return 0;
Expand Down Expand Up @@ -1292,7 +1296,7 @@ class LastMarkCmd extends MyCommand {

@Override
public int runCmd(CommandLine c) throws Exception {
LastMarkCommand command = new LastMarkCommand();
LastMarkCommand command = newLastMarkCommand();
command.apply(bkConf, new CliFlags());
return 0;
}
Expand Down Expand Up @@ -1351,12 +1355,12 @@ public int runCmd(CommandLine cmdLine) throws Exception {
return 1;
}

ListBookiesCommand.Flags flags = new ListBookiesCommand.Flags()
ListBookiesCommand.Flags flags = ListBookiesCommand.Flags.newFlags()
.readwrite(readwrite)
.readonly(readonly)
.all(all);

ListBookiesCommand command = new ListBookiesCommand(flags);
ListBookiesCommand command = newListBookiesCommand(flags);

command.apply(bkConf, flags);
return 0;
Expand Down Expand Up @@ -2552,7 +2556,7 @@ Options getOptions() {

@Override
int runCmd(CommandLine cmdLine) throws Exception {
ClusterInfoCommand cmd = new ClusterInfoCommand();
ClusterInfoCommand cmd = newClusterInfoCommand();
cmd.apply(bkConf, new CliFlags());
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class GarbageCollectorThread implements Runnable {

// Entry Logger Handle
final EntryLogger entryLogger;
final AbstractLogCompactor compactor;
AbstractLogCompactor compactor;

// Stats loggers for garbage collection operations
private final GarbageCollectorStats gcStats;
Expand Down Expand Up @@ -138,8 +138,12 @@ public GarbageCollectorThread(ServerConfiguration conf, LedgerManager ledgerMana
final CompactableLedgerStorage ledgerStorage,
EntryLogger entryLogger,
StatsLogger statsLogger) throws IOException {
this(conf, ledgerManager, ledgerDirsManager, ledgerStorage, entryLogger, statsLogger,
Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("GarbageCollectorThread")));
this(conf, ledgerManager, ledgerDirsManager, ledgerStorage, entryLogger, statsLogger, newExecutor());
}

@VisibleForTesting
static ScheduledExecutorService newExecutor() {
return Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("GarbageCollectorThread"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,18 +630,20 @@ static void writePaddingBytes(JournalChannel jc, ByteBuf paddingBuffer, int jour

// journal entry queue to commit
final BatchedBlockingQueue<QueueEntry> queue;
final BatchedBlockingQueue<ForceWriteRequest> forceWriteRequests;
BatchedBlockingQueue<ForceWriteRequest> forceWriteRequests;

volatile boolean running = true;
private final LedgerDirsManager ledgerDirsManager;
private final ByteBufAllocator allocator;
private final MemoryLimitController memoryLimitController;

// Expose Stats
private final JournalStats journalStats;

private JournalAliveListener journalAliveListener;

private MemoryLimitController memoryLimitController;


public Journal(int journalIndex, File journalDirectory, ServerConfiguration conf,
LedgerDirsManager ledgerDirsManager) {
this(journalIndex, journalDirectory, conf, ledgerDirsManager, NullStatsLogger.INSTANCE,
Expand Down Expand Up @@ -724,6 +726,14 @@ public Journal(int journalIndex, File journalDirectory, ServerConfiguration conf
this.journalAliveListener = journalAliveListener;
}

@VisibleForTesting
static Journal newJournal(int journalIndex, File journalDirectory, ServerConfiguration conf,
LedgerDirsManager ledgerDirsManager, StatsLogger statsLogger,
ByteBufAllocator allocator, JournalAliveListener journalAliveListener) {
return new Journal(journalIndex, journalDirectory, conf, ledgerDirsManager, statsLogger, allocator,
journalAliveListener);
}

JournalStats getJournalStats() {
return this.journalStats;
}
Expand Down Expand Up @@ -923,6 +933,14 @@ public int getJournalQueueLength() {
return queue.size();
}

@VisibleForTesting
JournalChannel newLogFile(long logId, Long replaceLogId) throws IOException {
return new JournalChannel(journalDirectory, logId, journalPreAllocSize, journalWriteBufferSize,
journalAlignmentSize, removePagesFromCache,
journalFormatVersionToWrite, getBufferedChannelBuilder(),
conf, fileChannelProvider, replaceLogId);
}

/**
* A thread used for persisting journal entries to journal files.
*
Expand Down Expand Up @@ -991,10 +1009,7 @@ public void run() {
? journalIds.get(0) : null;

journalCreationWatcher.reset().start();
logFile = new JournalChannel(journalDirectory, logId, journalPreAllocSize, journalWriteBufferSize,
journalAlignmentSize, removePagesFromCache,
journalFormatVersionToWrite, getBufferedChannelBuilder(),
conf, fileChannelProvider, replaceLogId);
logFile = newLogFile(logId, replaceLogId);

journalStats.getJournalCreationStats().registerSuccessfulEvent(
journalCreationWatcher.stop().elapsed(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
Expand Down Expand Up @@ -1275,4 +1290,14 @@ public void joinThread() throws InterruptedException {
long getMemoryUsage() {
return memoryLimitController.currentUsage();
}

@VisibleForTesting
void setMemoryLimitController(MemoryLimitController memoryLimitController) {
this.memoryLimitController = memoryLimitController;
}

@VisibleForTesting
public void setForceWriteRequests(BatchedBlockingQueue<ForceWriteRequest> forceWriteRequests) {
this.forceWriteRequests = forceWriteRequests;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.bookkeeper.bookie;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -51,6 +52,12 @@ public LegacyCookieValidation(ServerConfiguration conf,
this.registrationManager = registrationManager;
}

@VisibleForTesting
public static LegacyCookieValidation newLegacyCookieValidation(ServerConfiguration conf,
RegistrationManager registrationManager) {
return new LegacyCookieValidation(conf, registrationManager);
}

@Override
public void checkCookies(List<File> directories) throws BookieException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ public void initialize(ServerConfiguration conf,
statsLogger,
allocator);

this.scheduler = Executors.newSingleThreadScheduledExecutor(
this.scheduler = newScheduledExecutorService();
}

@VisibleForTesting
static ScheduledExecutorService newScheduledExecutorService() {
return Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setNameFormat("SortedLedgerStorage-%d")
.setPriority((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2).build());
.setNameFormat("SortedLedgerStorage-%d")
.setPriority((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2).build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ public SyncThread(ServerConfiguration conf,
this.dirsListener = dirsListener;
this.ledgerStorage = ledgerStorage;
this.checkpointSource = checkpointSource;
this.executor = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory(executorName));
this.executor = newExecutor();
this.syncExecutorTime = statsLogger.getThreadScopedCounter("sync-thread-time");
this.executor.submit(() -> ThreadRegistry.register(executorName, 0));
}

@VisibleForTesting
static ScheduledExecutorService newExecutor() {
return Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory(executorName));
}

@Override
public void startCheckpoint(Checkpoint checkpoint) {
doCheckpoint(checkpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static org.apache.bookkeeper.bookie.BookKeeperServerStats.JOURNAL_SYNC;
import static org.apache.bookkeeper.bookie.BookKeeperServerStats.JOURNAL_WRITE_BYTES;

import com.google.common.annotations.VisibleForTesting;
import java.util.function.Supplier;
import lombok.Getter;
import org.apache.bookkeeper.bookie.BookKeeperServerStats;
Expand Down Expand Up @@ -150,7 +151,7 @@ public class JournalStats {
name = JOURNAL_NUM_FLUSH_MAX_OUTSTANDING_BYTES,
help = "The number of journal flushes triggered by MAX_OUTSTANDING_BYTES"
)
private final Counter flushMaxOutstandingBytesCounter;
private Counter flushMaxOutstandingBytesCounter;
@StatsDoc(
name = JOURNAL_NUM_FLUSH_EMPTY_QUEUE,
help = "The number of journal flushes triggered when journal queue becomes empty"
Expand Down Expand Up @@ -222,4 +223,9 @@ public Long getSample() {
statsLogger.registerGauge(JOURNAL_MEMORY_USED, journalMemoryUsedStats);
}

@VisibleForTesting
public void setFlushMaxOutstandingBytesCounter(Counter flushMaxOutstandingBytesCounter) {
this.flushMaxOutstandingBytesCounter = flushMaxOutstandingBytesCounter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithMetadataBookieDriver;
import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithRegistrationManager;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -173,6 +174,12 @@ public BookKeeperAdmin(ClientConfiguration conf) throws IOException, Interrupted
this.mFactory = bkc.ledgerManagerFactory;
}

@VisibleForTesting
public static BookKeeperAdmin newBookKeeperAdmin(ClientConfiguration conf)
throws IOException, InterruptedException, BKException {
return new BookKeeperAdmin(conf);
}

/**
* Constructor that takes in a BookKeeper instance . This will be useful,
* when user already has bk instance ready.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2367,4 +2367,8 @@ void executeOrdered(Runnable runnable) throws RejectedExecutionException {
executor.execute(runnable);
}

@VisibleForTesting
public Queue<PendingAddOp> getPendingAddOps() {
return pendingAddOps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.apache.bookkeeper.bookie.BookKeeperServerStats.BOOKIE_SCOPE;

import com.google.common.annotations.VisibleForTesting;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.conf.ServerConfiguration;
Expand All @@ -30,6 +31,7 @@
import org.apache.bookkeeper.meta.exceptions.MetadataException;
import org.apache.bookkeeper.stats.StatsLogger;
import org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy;
import org.apache.zookeeper.ZooKeeper;

/**
* ZooKeeper based metadata bookie driver.
Expand Down Expand Up @@ -65,6 +67,12 @@ public synchronized MetadataBookieDriver initialize(ServerConfiguration conf,

@Override
public synchronized RegistrationManager createRegistrationManager() {
ZKRegistrationManager zkRegistrationManager = newZKRegistrationManager(serverConf, zk);
return zkRegistrationManager;
}

@VisibleForTesting
ZKRegistrationManager newZKRegistrationManager(ServerConfiguration serverConf, ZooKeeper zk) {
return new ZKRegistrationManager(serverConf, zk);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.bookkeeper.meta.zk;

import com.google.common.annotations.VisibleForTesting;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -31,6 +32,7 @@
import org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

/**
* ZooKeeper based metadata client driver.
Expand Down Expand Up @@ -76,15 +78,21 @@ public synchronized MetadataClientDriver initialize(ClientConfiguration conf,
@Override
public synchronized RegistrationClient getRegistrationClient() {
if (null == regClient) {
regClient = new ZKRegistrationClient(
zk,
ledgersRootPath,
scheduler,
bookieAddressTracking);
regClient = newZKRegistrationClient(
zk,
ledgersRootPath,
scheduler,
bookieAddressTracking);
}
return regClient;
}

@VisibleForTesting
ZKRegistrationClient newZKRegistrationClient(ZooKeeper zk, String ledgersRootPath,
ScheduledExecutorService scheduler, boolean bookieAddressTracking) {
return new ZKRegistrationClient(zk, ledgersRootPath, scheduler, bookieAddressTracking);
}

@Override
public synchronized void close() {
if (null != regClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public BookieServer(ServerConfiguration conf,
this.nettyServer.setRequestProcessor(this.requestProcessor);
}

@VisibleForTesting
public static BookieServer newBookieServer(ServerConfiguration conf,
Bookie bookie,
StatsLogger statsLogger,
ByteBufAllocator allocator,
UncleanShutdownDetection uncleanShutdownDetection)
throws CompatibilityException, UnavailableException, SecurityException, IOException,
InterruptedException, KeeperException, BookieException {
return new BookieServer(conf, bookie, statsLogger, allocator, uncleanShutdownDetection);
}

/**
* Currently the uncaught exception handler is used for DeathWatcher to notify
* lifecycle management that a bookie is dead for some reasons.
Expand Down
Loading

0 comments on commit ee56a95

Please sign in to comment.