Search in sources :

Example 76 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.

the class TestRSKilledWhenInitializing method startMaster.

/**
   * Start Master. Get as far as the state where Master is waiting on
   * RegionServers to check in, then return.
   */
private MasterThread startMaster(MasterThread master) {
    master.start();
    // It takes a while until ServerManager creation to happen inside Master startup.
    while (master.getMaster().getServerManager() == null) {
        continue;
    }
    // Set a listener for the waiting-on-RegionServers state. We want to wait
    // until this condition before we leave this method and start regionservers.
    final AtomicBoolean waiting = new AtomicBoolean(false);
    if (master.getMaster().getServerManager() == null)
        throw new NullPointerException("SM");
    master.getMaster().getServerManager().registerListener(new ServerListener() {

        @Override
        public void waiting() {
            waiting.set(true);
        }
    });
    // Wait until the Master gets to place where it is waiting on RegionServers to check in.
    while (!waiting.get()) {
        continue;
    }
    // Set the global master-is-active; gets picked up by regionservers later.
    masterActive.set(true);
    return master;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerListener(org.apache.hadoop.hbase.master.ServerListener)

Example 77 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.

the class TestRegionReplicaFailover method testSecondaryRegionKillWhilePrimaryIsAcceptingWrites.

/**
   * Tests the case where there are 3 region replicas and the primary is continuously accepting
   * new writes while one of the secondaries is killed. Verification is done for both of the
   * secondary replicas.
   */
@Test(timeout = 120000)
public void testSecondaryRegionKillWhilePrimaryIsAcceptingWrites() throws Exception {
    try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration());
        Table table = connection.getTable(htd.getTableName());
        Admin admin = connection.getAdmin()) {
        // start a thread to do the loading of primary
        // start with some base
        HTU.loadNumericRows(table, fam, 0, 1000);
        admin.flush(table.getName());
        HTU.loadNumericRows(table, fam, 1000, 2000);
        final AtomicReference<Throwable> ex = new AtomicReference<>(null);
        final AtomicBoolean done = new AtomicBoolean(false);
        final AtomicInteger key = new AtomicInteger(2000);
        Thread loader = new Thread() {

            @Override
            public void run() {
                while (!done.get()) {
                    try {
                        HTU.loadNumericRows(table, fam, key.get(), key.get() + 1000);
                        key.addAndGet(1000);
                    } catch (Throwable e) {
                        ex.compareAndSet(null, e);
                    }
                }
            }
        };
        loader.start();
        Thread aborter = new Thread() {

            @Override
            public void run() {
                try {
                    boolean aborted = false;
                    for (RegionServerThread rs : HTU.getMiniHBaseCluster().getRegionServerThreads()) {
                        for (Region r : rs.getRegionServer().getOnlineRegions(htd.getTableName())) {
                            if (r.getRegionInfo().getReplicaId() == 1) {
                                LOG.info("Aborting region server hosting secondary region replica");
                                rs.getRegionServer().abort("for test");
                                aborted = true;
                            }
                        }
                    }
                    assertTrue(aborted);
                } catch (Throwable e) {
                    ex.compareAndSet(null, e);
                }
            }

            ;
        };
        aborter.start();
        aborter.join();
        done.set(true);
        loader.join();
        assertNull(ex.get());
        // assert that the test is working as designed
        assertTrue(key.get() > 1000);
        LOG.info("Loaded up to key :" + key.get());
        verifyNumericRowsWithTimeout(table, fam, 0, key.get(), 0, 30000);
        verifyNumericRowsWithTimeout(table, fam, 0, key.get(), 1, 30000);
        verifyNumericRowsWithTimeout(table, fam, 0, key.get(), 2, 30000);
    }
    // restart the region server
    HTU.getMiniHBaseCluster().startRegionServer();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Table(org.apache.hadoop.hbase.client.Table) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(org.apache.hadoop.hbase.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) RegionServerThread(org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread) Admin(org.apache.hadoop.hbase.client.Admin) RegionServerThread(org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread) Test(org.junit.Test)

Example 78 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.

the class TestRegionReplicas method testFlushAndCompactionsInPrimary.

@Test(timeout = 300000)
public void testFlushAndCompactionsInPrimary() throws Exception {
    long runtime = 30 * 1000;
    // enable store file refreshing
    // 100ms refresh is a lot
    final int refreshPeriod = 100;
    HTU.getConfiguration().setInt("hbase.hstore.compactionThreshold", 3);
    HTU.getConfiguration().setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, refreshPeriod);
    // restart the region server so that it starts the refresher chore
    restartRegionServer();
    final int startKey = 0, endKey = 1000;
    try {
        openRegion(HTU, getRS(), hriSecondary);
        //load some data to primary so that reader won't fail
        HTU.loadNumericRows(table, f, startKey, endKey);
        TestRegionServerNoMaster.flushRegion(HTU, hriPrimary);
        // ensure that chore is run
        Threads.sleep(2 * refreshPeriod);
        final AtomicBoolean running = new AtomicBoolean(true);
        @SuppressWarnings("unchecked") final AtomicReference<Exception>[] exceptions = new AtomicReference[3];
        for (int i = 0; i < exceptions.length; i++) {
            exceptions[i] = new AtomicReference<>();
        }
        Runnable writer = new Runnable() {

            int key = startKey;

            @Override
            public void run() {
                try {
                    while (running.get()) {
                        byte[] data = Bytes.toBytes(String.valueOf(key));
                        Put put = new Put(data);
                        put.addColumn(f, null, data);
                        table.put(put);
                        key++;
                        if (key == endKey)
                            key = startKey;
                    }
                } catch (Exception ex) {
                    LOG.warn(ex);
                    exceptions[0].compareAndSet(null, ex);
                }
            }
        };
        Runnable flusherCompactor = new Runnable() {

            Random random = new Random();

            @Override
            public void run() {
                try {
                    while (running.get()) {
                        // flush or compact
                        if (random.nextBoolean()) {
                            TestRegionServerNoMaster.flushRegion(HTU, hriPrimary);
                        } else {
                            HTU.compact(table.getName(), random.nextBoolean());
                        }
                    }
                } catch (Exception ex) {
                    LOG.warn(ex);
                    exceptions[1].compareAndSet(null, ex);
                }
            }
        };
        Runnable reader = new Runnable() {

            Random random = new Random();

            @Override
            public void run() {
                try {
                    while (running.get()) {
                        // whether to do a close and open
                        if (random.nextInt(10) == 0) {
                            try {
                                closeRegion(HTU, getRS(), hriSecondary);
                            } catch (Exception ex) {
                                LOG.warn("Failed closing the region " + hriSecondary + " " + StringUtils.stringifyException(ex));
                                exceptions[2].compareAndSet(null, ex);
                            }
                            try {
                                openRegion(HTU, getRS(), hriSecondary);
                            } catch (Exception ex) {
                                LOG.warn("Failed opening the region " + hriSecondary + " " + StringUtils.stringifyException(ex));
                                exceptions[2].compareAndSet(null, ex);
                            }
                        }
                        int key = random.nextInt(endKey - startKey) + startKey;
                        assertGetRpc(hriSecondary, key, true);
                    }
                } catch (Exception ex) {
                    LOG.warn("Failed getting the value in the region " + hriSecondary + " " + StringUtils.stringifyException(ex));
                    exceptions[2].compareAndSet(null, ex);
                }
            }
        };
        LOG.info("Starting writer and reader");
        ExecutorService executor = Executors.newFixedThreadPool(3);
        executor.submit(writer);
        executor.submit(flusherCompactor);
        executor.submit(reader);
        // wait for threads
        Threads.sleep(runtime);
        running.set(false);
        executor.shutdown();
        executor.awaitTermination(30, TimeUnit.SECONDS);
        for (AtomicReference<Exception> exRef : exceptions) {
            Assert.assertNull(exRef.get());
        }
    } finally {
        HTU.deleteNumericRows(table, HConstants.CATALOG_FAMILY, startKey, endKey);
        closeRegion(HTU, getRS(), hriSecondary);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Put(org.apache.hadoop.hbase.client.Put) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 79 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project hbase by apache.

the class TestWALSplit method testLogCannotBeWrittenOnceParsed.

/**
   * Simulates splitting a WAL out from under a regionserver that is still trying to write it.
   * Ensures we do not lose edits.
   * @throws IOException
   * @throws InterruptedException
   */
@Test(timeout = 300000)
public void testLogCannotBeWrittenOnceParsed() throws IOException, InterruptedException {
    final AtomicLong counter = new AtomicLong(0);
    AtomicBoolean stop = new AtomicBoolean(false);
    // Region we'll write edits too and then later examine to make sure they all made it in.
    final String region = REGIONS.get(0);
    final int numWriters = 3;
    Thread zombie = new ZombieLastLogWriterRegionServer(counter, stop, region, numWriters);
    try {
        long startCount = counter.get();
        zombie.start();
        // Wait till writer starts going.
        while (startCount == counter.get()) Threads.sleep(1);
        // Give it a second to write a few appends.
        Threads.sleep(1000);
        final Configuration conf2 = HBaseConfiguration.create(conf);
        final User robber = User.createUserForTesting(conf2, ROBBER, GROUP);
        int count = robber.runAs(new PrivilegedExceptionAction<Integer>() {

            @Override
            public Integer run() throws Exception {
                StringBuilder ls = new StringBuilder("Contents of WALDIR (").append(WALDIR).append("):\n");
                for (FileStatus status : fs.listStatus(WALDIR)) {
                    ls.append("\t").append(status.toString()).append("\n");
                }
                LOG.debug(ls);
                LOG.info("Splitting WALs out from under zombie. Expecting " + numWriters + " files.");
                WALSplitter.split(HBASEDIR, WALDIR, OLDLOGDIR, fs, conf2, wals);
                LOG.info("Finished splitting out from under zombie.");
                Path[] logfiles = getLogForRegion(HBASEDIR, TABLE_NAME, region);
                assertEquals("wrong number of split files for region", numWriters, logfiles.length);
                int count = 0;
                for (Path logfile : logfiles) {
                    count += countWAL(logfile);
                }
                return count;
            }
        });
        LOG.info("zombie=" + counter.get() + ", robber=" + count);
        assertTrue("The log file could have at most 1 extra log entry, but can't have less. " + "Zombie could write " + counter.get() + " and logfile had only " + count, counter.get() == count || counter.get() + 1 == count);
    } finally {
        stop.set(true);
        zombie.interrupt();
        Threads.threadDumpingIsAlive(zombie);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) User(org.apache.hadoop.hbase.security.User) FileStatus(org.apache.hadoop.fs.FileStatus) Configuration(org.apache.hadoop.conf.Configuration) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) ByteString(org.apache.hadoop.hbase.shaded.com.google.protobuf.ByteString) LeaseExpiredException(org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException) CorruptedLogFileException(org.apache.hadoop.hbase.wal.WALSplitter.CorruptedLogFileException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) RemoteException(org.apache.hadoop.ipc.RemoteException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 80 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project hive by apache.

the class TestCompactor method schemaEvolutionAddColDynamicPartitioningInsert.

/**
   * Simple schema evolution add columns with partitioning.
   * @throws Exception
   */
@Test
public void schemaEvolutionAddColDynamicPartitioningInsert() throws Exception {
    String tblName = "dpct";
    List<String> colNames = Arrays.asList("a", "b");
    executeStatementOnDriver("drop table if exists " + tblName, driver);
    executeStatementOnDriver("CREATE TABLE " + tblName + "(a INT, b STRING) " + " PARTITIONED BY(ds string)" + //currently ACID requires table to be bucketed
    " CLUSTERED BY(a) INTO 2 BUCKETS" + " STORED AS ORC TBLPROPERTIES ('transactional'='true')", driver);
    // First INSERT round.
    executeStatementOnDriver("insert into " + tblName + " partition (ds) values (1, 'fred', " + "'today'), (2, 'wilma', 'yesterday')", driver);
    // ALTER TABLE ... ADD COLUMNS
    executeStatementOnDriver("ALTER TABLE " + tblName + " ADD COLUMNS(c int)", driver);
    // Validate there is an added NULL for column c.
    executeStatementOnDriver("SELECT * FROM " + tblName + " ORDER BY a", driver);
    ArrayList<String> valuesReadFromHiveDriver = new ArrayList<String>();
    driver.getResults(valuesReadFromHiveDriver);
    Assert.assertEquals(2, valuesReadFromHiveDriver.size());
    Assert.assertEquals("1\tfred\tNULL\ttoday", valuesReadFromHiveDriver.get(0));
    Assert.assertEquals("2\twilma\tNULL\tyesterday", valuesReadFromHiveDriver.get(1));
    // Second INSERT round with new inserts into previously existing partition 'yesterday'.
    executeStatementOnDriver("insert into " + tblName + " partition (ds) values " + "(3, 'mark', 1900, 'soon'), (4, 'douglas', 1901, 'last_century'), " + "(5, 'doc', 1902, 'yesterday')", driver);
    // Validate there the new insertions for column c.
    executeStatementOnDriver("SELECT * FROM " + tblName + " ORDER BY a", driver);
    valuesReadFromHiveDriver = new ArrayList<String>();
    driver.getResults(valuesReadFromHiveDriver);
    Assert.assertEquals(5, valuesReadFromHiveDriver.size());
    Assert.assertEquals("1\tfred\tNULL\ttoday", valuesReadFromHiveDriver.get(0));
    Assert.assertEquals("2\twilma\tNULL\tyesterday", valuesReadFromHiveDriver.get(1));
    Assert.assertEquals("3\tmark\t1900\tsoon", valuesReadFromHiveDriver.get(2));
    Assert.assertEquals("4\tdouglas\t1901\tlast_century", valuesReadFromHiveDriver.get(3));
    Assert.assertEquals("5\tdoc\t1902\tyesterday", valuesReadFromHiveDriver.get(4));
    Initiator initiator = new Initiator();
    initiator.setThreadId((int) initiator.getId());
    conf.setIntVar(HiveConf.ConfVars.HIVE_COMPACTOR_DELTA_NUM_THRESHOLD, 0);
    initiator.setHiveConf(conf);
    AtomicBoolean stop = new AtomicBoolean();
    stop.set(true);
    initiator.init(stop, new AtomicBoolean());
    initiator.run();
    TxnStore txnHandler = TxnUtils.getTxnStore(conf);
    ShowCompactResponse rsp = txnHandler.showCompact(new ShowCompactRequest());
    List<ShowCompactResponseElement> compacts = rsp.getCompacts();
    Assert.assertEquals(4, compacts.size());
    SortedSet<String> partNames = new TreeSet<String>();
    for (int i = 0; i < compacts.size(); i++) {
        Assert.assertEquals("default", compacts.get(i).getDbname());
        Assert.assertEquals(tblName, compacts.get(i).getTablename());
        Assert.assertEquals("initiated", compacts.get(i).getState());
        partNames.add(compacts.get(i).getPartitionname());
    }
    List<String> names = new ArrayList<String>(partNames);
    Assert.assertEquals("ds=last_century", names.get(0));
    Assert.assertEquals("ds=soon", names.get(1));
    Assert.assertEquals("ds=today", names.get(2));
    Assert.assertEquals("ds=yesterday", names.get(3));
    // Validate after compaction.
    executeStatementOnDriver("SELECT * FROM " + tblName + " ORDER BY a", driver);
    valuesReadFromHiveDriver = new ArrayList<String>();
    driver.getResults(valuesReadFromHiveDriver);
    Assert.assertEquals(5, valuesReadFromHiveDriver.size());
    Assert.assertEquals("1\tfred\tNULL\ttoday", valuesReadFromHiveDriver.get(0));
    Assert.assertEquals("2\twilma\tNULL\tyesterday", valuesReadFromHiveDriver.get(1));
    Assert.assertEquals("3\tmark\t1900\tsoon", valuesReadFromHiveDriver.get(2));
    Assert.assertEquals("4\tdouglas\t1901\tlast_century", valuesReadFromHiveDriver.get(3));
    Assert.assertEquals("5\tdoc\t1902\tyesterday", valuesReadFromHiveDriver.get(4));
}
Also used : ArrayList(java.util.ArrayList) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ShowCompactResponse(org.apache.hadoop.hive.metastore.api.ShowCompactResponse) TreeSet(java.util.TreeSet) ShowCompactRequest(org.apache.hadoop.hive.metastore.api.ShowCompactRequest) TxnStore(org.apache.hadoop.hive.metastore.txn.TxnStore) ShowCompactResponseElement(org.apache.hadoop.hive.metastore.api.ShowCompactResponseElement) Test(org.junit.Test)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2412 Test (org.junit.Test)1002 CountDownLatch (java.util.concurrent.CountDownLatch)394 IOException (java.io.IOException)336 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)301 ArrayList (java.util.ArrayList)214 AtomicReference (java.util.concurrent.atomic.AtomicReference)202 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)108 Test (org.testng.annotations.Test)106 List (java.util.List)98 Ignite (org.apache.ignite.Ignite)98 AtomicLong (java.util.concurrent.atomic.AtomicLong)94 HashMap (java.util.HashMap)93 ExecutorService (java.util.concurrent.ExecutorService)90 Map (java.util.Map)88 ExecutionException (java.util.concurrent.ExecutionException)87 File (java.io.File)68 Random (java.util.Random)68 CyclicBarrier (java.util.concurrent.CyclicBarrier)68 HashSet (java.util.HashSet)63