Search in sources :

Example 21 with BookieSocketAddress

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);
}
Also used : GetBookieInfoCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback) BookieClient(org.apache.bookkeeper.proto.BookieClient) BookieInfo(org.apache.bookkeeper.client.BookieInfoReader.BookieInfo) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 22 with BookieSocketAddress

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;
    });
}
Also used : LedgerManager(org.apache.bookkeeper.meta.LedgerManager) LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Cleanup(lombok.Cleanup) TestCallbacks(org.apache.bookkeeper.test.TestCallbacks) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) Test(org.junit.Test)

Example 23 with BookieSocketAddress

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());
}
Also used : LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) TestCallbacks(org.apache.bookkeeper.test.TestCallbacks)

Example 24 with BookieSocketAddress

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 25 with BookieSocketAddress

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);
    }
}
Also used : RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) Set(java.util.Set) InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)254 Test (org.junit.Test)140 HashSet (java.util.HashSet)67 CountDownLatch (java.util.concurrent.CountDownLatch)42 ArrayList (java.util.ArrayList)40 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)38 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)37 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)29 HashMap (java.util.HashMap)28 Map (java.util.Map)24 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)23 IOException (java.io.IOException)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 BookieServer (org.apache.bookkeeper.proto.BookieServer)14 WriteCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback)13 Set (java.util.Set)11 ByteBuf (io.netty.buffer.ByteBuf)10 ByteBuffer (java.nio.ByteBuffer)10 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10