use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class BookieClientTest method testGetBookieInfo.
@Test
public void testGetBookieInfo() throws IOException, InterruptedException {
BookieSocketAddress addr = bs.getLocalAddress();
BookieClient bc = new BookieClient(new ClientConfiguration(), new NioEventLoopGroup(), executor, scheduler, NullStatsLogger.INSTANCE);
long flags = BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE | BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE;
class CallbackObj {
int rc;
long requested;
long freeDiskSpace, totalDiskCapacity;
CountDownLatch latch = new CountDownLatch(1);
CallbackObj(long requested) {
this.requested = requested;
this.rc = 0;
this.freeDiskSpace = 0L;
this.totalDiskCapacity = 0L;
}
}
CallbackObj obj = new CallbackObj(flags);
bc.getBookieInfo(addr, flags, new GetBookieInfoCallback() {
@Override
public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) {
CallbackObj obj = (CallbackObj) ctx;
obj.rc = rc;
if (rc == Code.OK) {
if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) {
obj.freeDiskSpace = bInfo.getFreeDiskSpace();
}
if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) {
obj.totalDiskCapacity = bInfo.getTotalDiskSpace();
}
}
obj.latch.countDown();
}
}, obj);
obj.latch.await();
System.out.println("Return code: " + obj.rc + "FreeDiskSpace: " + obj.freeDiskSpace + " TotalCapacity: " + obj.totalDiskCapacity);
assertTrue("GetBookieInfo failed with error " + obj.rc, obj.rc == Code.OK);
assertTrue("GetBookieInfo failed with error " + obj.rc, obj.freeDiskSpace <= obj.totalDiskCapacity);
assertTrue("GetBookieInfo failed with error " + obj.rc, obj.totalDiskCapacity > 0);
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class AuditorPeriodicBookieCheckTest method testPeriodicBookieCheckInterval.
/**
* Test that the periodic bookie checker works.
*/
@Test
public void testPeriodicBookieCheckInterval() throws Exception {
bsConfs.get(0).setZkServers(zkUtil.getZooKeeperConnectString());
runFunctionWithLedgerManagerFactory(bsConfs.get(0), mFactory -> {
try (LedgerManager ledgerManager = mFactory.newLedgerManager()) {
@Cleanup final LedgerUnderreplicationManager underReplicationManager = mFactory.newLedgerUnderreplicationManager();
LedgerHandle lh = bkc.createLedger(3, 3, DigestType.CRC32, "passwd".getBytes());
LedgerMetadata md = LedgerHandleAdapter.getLedgerMetadata(lh);
List<BookieSocketAddress> ensemble = md.getEnsembles().get(0L);
ensemble.set(0, new BookieSocketAddress("1.1.1.1", 1000));
TestCallbacks.GenericCallbackFuture<Void> cb = new TestCallbacks.GenericCallbackFuture<Void>();
ledgerManager.writeLedgerMetadata(lh.getId(), md, cb);
cb.get();
long underReplicatedLedger = -1;
for (int i = 0; i < 10; i++) {
underReplicatedLedger = underReplicationManager.pollLedgerToRereplicate();
if (underReplicatedLedger != -1) {
break;
}
Thread.sleep(CHECK_INTERVAL * 1000);
}
assertEquals("Ledger should be under replicated", lh.getId(), underReplicatedLedger);
} catch (Exception e) {
throw new UncheckedExecutionException(e.getMessage(), e);
}
return null;
});
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class AuditorRollingRestartTest method testAuditingDuringRollingRestart.
private void testAuditingDuringRollingRestart(LedgerManagerFactory mFactory) throws Exception {
final LedgerUnderreplicationManager underReplicationManager = mFactory.newLedgerUnderreplicationManager();
LedgerHandle lh = bkc.createLedger(3, 3, DigestType.CRC32, "passwd".getBytes());
for (int i = 0; i < 10; i++) {
lh.asyncAddEntry("foobar".getBytes(), new TestCallbacks.AddCallbackFuture(i), null);
}
lh.addEntry("foobar".getBytes());
lh.close();
assertEquals("shouldn't be anything under replicated", underReplicationManager.pollLedgerToRereplicate(), -1);
underReplicationManager.disableLedgerReplication();
BookieSocketAddress auditor = AuditorElector.getCurrentAuditor(baseConf, zkc);
ServerConfiguration conf = killBookie(auditor);
Thread.sleep(2000);
startBookie(conf);
// give it time to run
Thread.sleep(2000);
assertEquals("shouldn't be anything under replicated", -1, underReplicationManager.pollLedgerToRereplicate());
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class Bookie method getBookieAddress.
/**
* Return the configured address of the bookie.
*/
public static BookieSocketAddress getBookieAddress(ServerConfiguration conf) throws UnknownHostException {
// useHostNameAsBookieID settings
if (conf.getAdvertisedAddress() != null && conf.getAdvertisedAddress().trim().length() > 0) {
String hostAddress = conf.getAdvertisedAddress().trim();
return new BookieSocketAddress(hostAddress, conf.getBookiePort());
}
String iface = conf.getListeningInterface();
if (iface == null) {
iface = "default";
}
String hostName = DNS.getDefaultHost(iface);
InetSocketAddress inetAddr = new InetSocketAddress(hostName, conf.getBookiePort());
if (inetAddr.isUnresolved()) {
throw new UnknownHostException("Unable to resolve default hostname: " + hostName + " for interface: " + iface);
}
String hostAddress = null;
InetAddress iAddress = inetAddr.getAddress();
if (conf.getUseHostNameAsBookieID()) {
hostAddress = iAddress.getCanonicalHostName();
if (conf.getUseShortHostName()) {
/*
* if short hostname is used, then FQDN is not used. Short
* hostname is the hostname cut at the first dot.
*/
hostAddress = hostAddress.split("\\.", 2)[0];
}
} else {
hostAddress = iAddress.getHostAddress();
}
BookieSocketAddress addr = new BookieSocketAddress(hostAddress, conf.getBookiePort());
if (addr.getSocketAddress().getAddress().isLoopbackAddress() && !conf.getAllowLoopback()) {
throw new UnknownHostException("Trying to listen on loopback address, " + addr + " but this is forbidden by default " + "(see ServerConfiguration#getAllowLoopback())");
}
return addr;
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class Bookie method checkEnvironmentWithStorageExpansion.
public static void checkEnvironmentWithStorageExpansion(ServerConfiguration conf, MetadataBookieDriver metadataDriver, List<File> journalDirectories, List<File> allLedgerDirs) throws BookieException {
RegistrationManager rm = metadataDriver.getRegistrationManager();
try {
// 1. retrieve the instance id
String instanceId = rm.getClusterInstanceId();
// 2. build the master cookie from the configuration
Cookie.Builder builder = Cookie.generateCookie(conf);
if (null != instanceId) {
builder.setInstanceId(instanceId);
}
Cookie masterCookie = builder.build();
boolean allowExpansion = conf.getAllowStorageExpansion();
// 3. read the cookie from registration manager. it is the `source-of-truth` of a given bookie.
// if it doesn't exist in registration manager, this bookie is a new bookie, otherwise it is
// an old bookie.
List<BookieSocketAddress> possibleBookieIds = possibleBookieIds(conf);
final Versioned<Cookie> rmCookie = readAndVerifyCookieFromRegistrationManager(masterCookie, rm, possibleBookieIds, allowExpansion);
// 4. check if the cookie appear in all the directories.
List<File> missedCookieDirs = new ArrayList<>();
List<Cookie> existingCookies = Lists.newArrayList();
if (null != rmCookie) {
existingCookies.add(rmCookie.getValue());
}
// 4.1 verify the cookies in journal directories
Pair<List<File>, List<Cookie>> journalResult = verifyAndGetMissingDirs(masterCookie, allowExpansion, journalDirectories);
missedCookieDirs.addAll(journalResult.getLeft());
existingCookies.addAll(journalResult.getRight());
// 4.2. verify the cookies in ledger directories
Pair<List<File>, List<Cookie>> ledgerResult = verifyAndGetMissingDirs(masterCookie, allowExpansion, allLedgerDirs);
missedCookieDirs.addAll(ledgerResult.getLeft());
existingCookies.addAll(ledgerResult.getRight());
// - a directory has been corrupted/wiped, which is an error
if (!missedCookieDirs.isEmpty()) {
if (rmCookie == null) {
// 5.1 new environment: all directories should be empty
verifyDirsForNewEnvironment(missedCookieDirs);
stampNewCookie(conf, masterCookie, rm, Version.NEW, journalDirectories, allLedgerDirs);
} else if (allowExpansion) {
// 5.2 storage is expanding
Set<File> knownDirs = getKnownDirs(existingCookies);
verifyDirsForStorageExpansion(missedCookieDirs, knownDirs);
stampNewCookie(conf, masterCookie, rm, rmCookie.getVersion(), journalDirectories, allLedgerDirs);
} else {
// 5.3 Cookie-less directories and
// we can't do anything with them
LOG.error("There are directories without a cookie," + " and this is neither a new environment," + " nor is storage expansion enabled. " + "Empty directories are {}", missedCookieDirs);
throw new InvalidCookieException();
}
}
} catch (IOException ioe) {
LOG.error("Error accessing cookie on disks", ioe);
throw new BookieException.InvalidCookieException(ioe);
}
}
Aggregations