org.apache.commons.lang3.mutable.MutableBoolean Java Examples

The following examples show how to use org.apache.commons.lang3.mutable.MutableBoolean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleEnqueueDequeueThenBlock() throws Exception
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  sdqqm.enqueue(query, null, new MutableBoolean(false));

  Assert.assertEquals(1, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());

  QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeueBlock();

  Assert.assertEquals(0, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());

  testBlocking(sdqqm);

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #2
Source File: FilterableListContainerTest.java    From viritin with Apache License 2.0 6 votes vote down vote up
@Test
public void clearFilters() {
    final List<Person> listOfPersons = getListOfPersons(100);
    FilterableListContainer<Person> container = new FilterableListContainer<>(
            listOfPersons);
    container.addContainerFilter(new SimpleStringFilter("firstName",
            "First1", true, true));
    Assert.assertNotSame(listOfPersons.size(), container.size());
    container.removeAllContainerFilters();
    Assert.assertEquals(listOfPersons.size(), container.size());
    container.addContainerFilter(new SimpleStringFilter("firstName",
            "foobar", true, true));
    Assert.assertEquals(0, container.size());

    final MutableBoolean fired = new MutableBoolean(false);
    container.addListener(new Container.ItemSetChangeListener() {
        @Override
        public void containerItemSetChange(
                Container.ItemSetChangeEvent event) {
            fired.setTrue();
        }
    });
    container.removeAllContainerFilters();
    Assert.assertTrue(fired.booleanValue());
    Assert.assertEquals(listOfPersons.size(), container.size());
}
 
Example #3
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleEnqueueDequeueBlock()
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  sdqqm.enqueue(query, null, new MutableBoolean(false));

  Assert.assertEquals(1, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());

  QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeueBlock();

  Assert.assertEquals(0, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());

  Assert.assertEquals("Should return same query.", query, qb.getQuery());

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #4
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Test
public void firstDoneTest()
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  sdqqm.enqueue(query, null, new MutableBoolean(true));

  QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.beginWindow(1);

  qb = sdqqm.dequeue();

  Assert.assertEquals("Should return back null.", null, qb);
  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #5
Source File: TransferTest.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationFailureForMissingRequestContextAttribute() {
 MutableBoolean exceptionHandled = new MutableBoolean(false);
 LogEventFactory.setDefaultHandler((message, ex) -> {
  assertThat(ex, instanceOf(ConstraintValidationException.class));
  exceptionHandled.setTrue();
 });

    Transfer transfer = LogEventFactory.getEvent(Transfer.class);
    ThreadContext.put("companyId", "12345");
    ThreadContext.put("ipAddress", "127.0.0.1");
    ThreadContext.put("environment", "dev");
    ThreadContext.put("product", "TestProduct");
    ThreadContext.put("timeZone", "America/Phoenix");
    ThreadContext.put("loginId", "TestUser");
    transfer.setToAccount(123456);
    transfer.setFromAccount(111111);
    transfer.setAmount(new BigDecimal(111.55));
    transfer.logEvent();

 assertTrue("Should have thrown a ConstraintValidationException", exceptionHandled.isTrue());
}
 
Example #6
Source File: TransferTest.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationFailureForMissingEventAttribute() {
 MutableBoolean exceptionHandled = new MutableBoolean(false);
 LogEventFactory.setDefaultHandler((message, ex) -> {
  assertThat(ex, instanceOf(ConstraintValidationException.class));
  exceptionHandled.setTrue();
 });

    Transfer transfer = LogEventFactory.getEvent(Transfer.class);
    ThreadContext.put("accountNumber", "12345");
    ThreadContext.put("companyId", "12345");
    ThreadContext.put("userId", "JohnDoe");
    ThreadContext.put("ipAddress", "127.0.0.1");
    ThreadContext.put("environment", "dev");
    ThreadContext.put("product", "TestProduct");
    ThreadContext.put("timeZone", "America/Phoenix");
    ThreadContext.put("loginId", "TestUser");
    transfer.setToAccount(123456);
    transfer.setFromAccount(111111);
    transfer.logEvent();

 assertTrue("Should have thrown a ConstraintValidationException", exceptionHandled.isTrue());
}
 
Example #7
Source File: TransferTest.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomExceptionHandlerIsPassedToEvent() {
    AbstractConfiguration config = setUpFailingAppender();

    MutableBoolean exceptionHandled = new MutableBoolean(false);
 LogEventFactory.setDefaultHandler((message, ex) -> {
     assertThat(ex, instanceOf(LoggingException.class));
     exceptionHandled.setTrue();
 });

    Transfer transfer = setUpMinimumEvent();
    transfer.logEvent();

    assertTrue("Exception was not handled through the custom handler", exceptionHandled.isTrue());

    config.removeAppender(failingAppenderName);
}
 
Example #8
Source File: SchemaTableTree.java    From sqlg with MIT License 6 votes vote down vote up
private String toRangeClause(SqlgGraph sqlgGraph, MutableBoolean mutableOrderBy) {
    if (this.sqlgRangeHolder != null && this.sqlgRangeHolder.isApplyOnDb()) {
        if (this.sqlgRangeHolder.hasRange()) {
            //This is MssqlServer, ugly but what to do???
            String sql = "";
            if (mutableOrderBy.isFalse() && sqlgGraph.getSqlDialect().isMssqlServer() && this.getDbComparators().isEmpty()) {
                sql = "\n\tORDER BY 1\n\t";
            }
            return sql + "\n" + sqlgGraph.getSqlDialect().getRangeClause(this.sqlgRangeHolder.getRange());
        } else {
            Preconditions.checkState(this.sqlgRangeHolder.hasSkip(), "If not a range query then it must be a skip.");
            return sqlgGraph.getSqlDialect().getSkipClause(this.sqlgRangeHolder.getSkip());
        }
    }
    return "";
}
 
Example #9
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void expiredTestBlocking() throws Exception
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  MutableBoolean queueContext = new MutableBoolean(false);
  sdqqm.enqueue(query, null, queueContext);

  Assert.assertEquals(1, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());
  QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeueBlock();
  Assert.assertEquals(0, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());

  sdqqm.endWindow();

  sdqqm.beginWindow(1);

  Assert.assertEquals(1, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());

  queueContext.setValue(true);
  testBlocking(sdqqm);

  Assert.assertEquals(0, sdqqm.getNumLeft());
  Assert.assertEquals(sdqqm.getNumPermits(), sdqqm.getNumLeft());

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #10
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleExpire1()
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  sdqqm.enqueue(query, null, new MutableBoolean(false));

  QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeue();

  Assert.assertEquals("Should return same query.", query, qb.getQuery());
  qb.getQueueContext().setValue(true);

  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.beginWindow(1);

  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #11
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleEnqueueDequeue()
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  sdqqm.enqueue(query, null, new MutableBoolean(false));

  QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeue();

  Assert.assertEquals("Should return same query.", query, qb.getQuery());
  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.beginWindow(1);

  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return same query.", query, qb.getQuery());
  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #12
Source File: HashJoinBatch.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Used to fetch the first data holding batch from either the build or probe side.
 * @param outcome The current upstream outcome for either the build or probe side.
 * @param prefetched A flag indicating if we have already done a prefetch of the first data holding batch for the probe or build side.
 * @param isEmpty A flag indicating if the probe or build side is empty.
 * @param index The upstream index of the probe or build batch.
 * @param batch The probe or build batch itself.
 * @param memoryManagerUpdate A lambda function to execute the memory manager update for the probe or build batch.
 * @return The current {@link org.apache.drill.exec.record.RecordBatch.IterOutcome}.
 */
private IterOutcome prefetchFirstBatch(IterOutcome outcome,
                                       final MutableBoolean prefetched,
                                       final MutableBoolean isEmpty,
                                       final int index,
                                       final RecordBatch batch,
                                       final Runnable memoryManagerUpdate) {
  if (prefetched.booleanValue()) {
    // We have already prefetch the first data holding batch
    return outcome;
  }

  // If we didn't retrieve our first data holding batch, we need to do it now.
  prefetched.setValue(true);

  if (outcome != IterOutcome.NONE) {
    // We can only get data if there is data available
    outcome = sniffNonEmptyBatch(outcome, index, batch);
  }

  isEmpty.setValue(outcome == IterOutcome.NONE); // If we recieved NONE there is no data.

  if (outcome == IterOutcome.OUT_OF_MEMORY) {
    // We reached a termination state
    state = BatchState.OUT_OF_MEMORY;
  } else if (outcome == IterOutcome.STOP) {
    // We reached a termination state
    state = BatchState.STOP;
  } else {
    // Got our first batch(es)
    if (spilledState.isFirstCycle()) {
      // Only collect stats for the first cylce
      memoryManagerUpdate.run();
    }
    state = BatchState.FIRST;
  }

  return outcome;
}
 
Example #13
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleExpire1ThenBlock()
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  sdqqm.enqueue(query, null, new MutableBoolean(false));

  QueryBundle<Query, Void, MutableBoolean> qb = sdqqm.dequeue();

  Assert.assertEquals("Should return same query.", query, qb.getQuery());
  qb.getQueueContext().setValue(true);

  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.beginWindow(1);

  qb = sdqqm.dequeue();
  Assert.assertEquals("Should return back null.", null, qb);

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #14
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleExpireBlockThenUnblock() throws Exception
{
  SimpleDoneQueueManager<Query, Void> sdqqm = new SimpleDoneQueueManager<Query, Void>();

  sdqqm.setup(null);
  sdqqm.beginWindow(0);

  Query query = new MockQuery("1");
  MutableBoolean expire = new MutableBoolean(false);
  sdqqm.enqueue(query, null, expire);

  sdqqm.endWindow();
  sdqqm.beginWindow(1);

  //Expire
  expire.setValue(true);

  ExceptionSaverExceptionHandler eseh = new ExceptionSaverExceptionHandler();
  testBlockingNoStop(sdqqm, eseh);

  query = new MockQuery("2");
  sdqqm.enqueue(query, null, new MutableBoolean(false));

  Thread.sleep(1000);

  Assert.assertNull(eseh.getCaughtThrowable());

  sdqqm.endWindow();
  sdqqm.teardown();
}
 
Example #15
Source File: SimpleDoneQueryQueueManagerTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"deprecation", "CallToThreadStopSuspendOrResumeManager"})
private void testBlocking(SimpleDoneQueueManager<Query, Void> sdqqm) throws InterruptedException
{
  Thread thread = new Thread(new BlockedThread<Query, Void, MutableBoolean>(sdqqm));
  //thread.setUncaughtExceptionHandler(new RethrowExceptionHandler(Thread.currentThread()));
  thread.start();
  Thread.sleep(100);

  Assert.assertEquals(Thread.State.WAITING, thread.getState());

  thread.stop();
}
 
Example #16
Source File: TransferTest.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidationFailureForInvalidRequestContextAttribute() {
    MutableBoolean exceptionHandled = new MutableBoolean(false);
    LogEventFactory.setDefaultHandler((message, ex) -> {
        assertThat(ex, instanceOf(ConstraintValidationException.class));
        exceptionHandled.setTrue();
    });

    Transfer transfer = LogEventFactory.getEvent(Transfer.class);
    transfer.setToAccount(0);

    assertTrue("Should have thrown a ConstraintValidationException", exceptionHandled.isTrue());
}
 
Example #17
Source File: ScapManager.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private static String truncate(String string, int maxLen, MutableBoolean truncated) {
    if (string != null && string.length() > maxLen) {
        truncated.setValue(true);
        return string.substring(0, maxLen - 3) + "...";
    }
    return string;
}
 
Example #18
Source File: ScapManager.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private static XccdfBenchmark getOrCreateBenchmark(BenchmarkResume resume,
                                                   MutableBoolean truncated) {
    long benchId = lookupBenchmark(truncate(resume.getId(), 120, truncated),
            truncate(resume.getVersion(), 80, truncated));
    return ScapFactory.lookupBenchmarkById(benchId)
            .orElseThrow(() ->
                    new RuntimeException(
                            "Xccdf benchmark not found in db identifier=" +
                                    resume.getId() +
                                    ", version=" +
                                    resume.getVersion()));
}
 
Example #19
Source File: ScapManager.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private static XccdfProfile getOrCreateProfile(Profile profile,
                                               MutableBoolean truncated) {
    long profileId = lookupProfile(profile.getId(),
            truncate(profile.getTitle(),
                    120, truncated));
    return ScapFactory.lookupProfileById(profileId)
            .orElseThrow(() ->
                    new RuntimeException(
                            "Xccdf benchmark not found in db identifier=" +
                                    profile.getId() +
                                    ", version=" +
                                    profile.getTitle()));
}
 
Example #20
Source File: ScapManager.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private static void processRuleResult(XccdfTestResult testResult,
                                      List<TestResultRuleResult> ruleResults,
                                      String label,
                                      MutableBoolean truncated) {
    for (TestResultRuleResult rr : ruleResults) {
        XccdfRuleResult ruleResult = new XccdfRuleResult();
        ruleResult.setTestResult(testResult);
        testResult.getResults().add(ruleResult);
        Optional<XccdfRuleResultType> resultType
                = ScapFactory.lookupRuleResultType(label);
        ruleResult.setResultType(
                resultType.orElseThrow(() ->
                        new RuntimeException("no xccdf result type found for label=" +
                                label)));
        ruleResult.getIdents().add(
                getOrCreateIdent("#IDREF#", truncate(rr.getId(), 255, truncated)));
        if (rr.getIdents() != null) {
            for (TestResultRuleResultIdent rrIdent : rr.getIdents()) {
                String text = truncate(rrIdent.getText(), 255, truncated);
                if (StringUtils.isEmpty(text)) {
                    continue;
                }
                ruleResult.getIdents().add(
                        getOrCreateIdent(
                                rrIdent.getSystem(),
                                text));
            }

        }
    }
}
 
Example #21
Source File: AsyncBatchRpcRetryingCaller.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void onComplete(Map<byte[], RegionRequest> actionsByRegion, int tries,
    ServerName serverName, MultiResponse resp) {
  ConnectionUtils.updateStats(conn.getStatisticsTracker(), conn.getConnectionMetrics(),
    serverName, resp);
  List<Action> failedActions = new ArrayList<>();
  MutableBoolean retryImmediately = new MutableBoolean(false);
  actionsByRegion.forEach((rn, regionReq) -> {
    RegionResult regionResult = resp.getResults().get(rn);
    Throwable regionException = resp.getException(rn);
    if (regionResult != null) {
      regionReq.actions.forEach(action -> onComplete(action, regionReq, tries, serverName,
        regionResult, failedActions, regionException, retryImmediately));
    } else {
      Throwable error;
      if (regionException == null) {
        LOG.error("Server sent us neither results nor exceptions for {}",
          Bytes.toStringBinary(rn));
        error = new RuntimeException("Invalid response");
      } else {
        error = translateException(regionException);
      }
      logException(tries, () -> Stream.of(regionReq), error, serverName);
      conn.getLocator().updateCachedLocationOnError(regionReq.loc, error);
      if (error instanceof DoNotRetryIOException || tries >= maxAttempts) {
        failAll(regionReq.actions.stream(), tries, error, serverName);
        return;
      }
      if (!retryImmediately.booleanValue() && error instanceof RetryImmediatelyException) {
        retryImmediately.setTrue();
      }
      addError(regionReq.actions, error, serverName);
      failedActions.addAll(regionReq.actions);
    }
  });
  if (!failedActions.isEmpty()) {
    tryResubmit(failedActions.stream(), tries, retryImmediately.booleanValue(), false);
  }
}
 
Example #22
Source File: NestedSteps.java    From vividus with Apache License 2.0 5 votes vote down vote up
private boolean isExpectedElementsQuantity(SearchAttributes locator, Matcher<Integer> elementsMatcher,
        MutableBoolean firstIteration)
{
    if (firstIteration.isTrue())
    {
        firstIteration.setValue(false);
        return softAssert.assertThat("Elements number", getElementsNumber(locator), elementsMatcher);
    }
    return elementsMatcher.matches(getElementsNumber(locator));
}
 
Example #23
Source File: ScheduleUtils.java    From pacaya with Apache License 2.0 5 votes vote down vote up
public static <T> Iterable<T> iterable(Iterator<T> seq) {
    final MutableBoolean used = new MutableBoolean(false);
    return new Iterable<T>() {

        @Override
        public Iterator<T> iterator() {
            if (!used.booleanValue()) {
                used.setValue(true);
                return seq;
            } else {
                throw new IllegalStateException("only allowed to iterate this iterable once");
            }
        }
    };
}
 
Example #24
Source File: KinesisDataFetcherTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testPeriodicWatermark() {
	final MutableLong clock = new MutableLong();
	final MutableBoolean isTemporaryIdle = new MutableBoolean();
	final List<Watermark> watermarks = new ArrayList<>();

	String fakeStream1 = "fakeStream1";
	StreamShardHandle shardHandle =
		new StreamShardHandle(
			fakeStream1,
			new Shard().withShardId(KinesisShardIdGenerator.generateFromShardOrder(0)));

	TestSourceContext<String> sourceContext =
		new TestSourceContext<String>() {
			@Override
			public void emitWatermark(Watermark mark) {
				watermarks.add(mark);
			}

			@Override
			public void markAsTemporarilyIdle() {
				isTemporaryIdle.setTrue();
			}
		};

	HashMap<String, String> subscribedStreamsToLastSeenShardIdsUnderTest = new HashMap<>();

	final KinesisDataFetcher<String> fetcher =
		new TestableKinesisDataFetcher<String>(
			Collections.singletonList(fakeStream1),
			sourceContext,
			new java.util.Properties(),
			new KinesisDeserializationSchemaWrapper<>(new org.apache.flink.streaming.util.serialization.SimpleStringSchema()),
			1,
			1,
			new AtomicReference<>(),
			new LinkedList<>(),
			subscribedStreamsToLastSeenShardIdsUnderTest,
			FakeKinesisBehavioursFactory.nonReshardedStreamsBehaviour(new HashMap<>())) {

			@Override
			protected long getCurrentTimeMillis() {
				return clock.getValue();
			}
		};
	Whitebox.setInternalState(fetcher, "periodicWatermarkAssigner", watermarkAssigner);

	SequenceNumber seq = new SequenceNumber("fakeSequenceNumber");
	// register shards to subsequently emit records
	int shardIndex =
		fetcher.registerNewSubscribedShardState(
			new KinesisStreamShardState(
				KinesisDataFetcher.convertToStreamShardMetadata(shardHandle), shardHandle, seq));

	StreamRecord<String> record1 =
		new StreamRecord<>(String.valueOf(Long.MIN_VALUE), Long.MIN_VALUE);
	fetcher.emitRecordAndUpdateState(record1.getValue(), record1.getTimestamp(), shardIndex, seq);
	Assert.assertEquals(record1, sourceContext.getCollectedOutputs().poll());

	fetcher.emitWatermark();
	Assert.assertTrue("potential watermark equals previous watermark", watermarks.isEmpty());

	StreamRecord<String> record2 = new StreamRecord<>(String.valueOf(1), 1);
	fetcher.emitRecordAndUpdateState(record2.getValue(), record2.getTimestamp(), shardIndex, seq);
	Assert.assertEquals(record2, sourceContext.getCollectedOutputs().poll());

	fetcher.emitWatermark();
	Assert.assertFalse("watermark advanced", watermarks.isEmpty());
	Assert.assertEquals(new Watermark(record2.getTimestamp()), watermarks.remove(0));
	Assert.assertFalse("not idle", isTemporaryIdle.booleanValue());

	// test idle timeout
	long idleTimeout = 10;
	// advance clock idleTimeout
	clock.add(idleTimeout + 1);
	fetcher.emitWatermark();
	Assert.assertFalse("not idle", isTemporaryIdle.booleanValue());
	Assert.assertTrue("not idle, no new watermark", watermarks.isEmpty());

	// activate idle timeout
	Whitebox.setInternalState(fetcher, "shardIdleIntervalMillis", idleTimeout);
	fetcher.emitWatermark();
	Assert.assertTrue("idle", isTemporaryIdle.booleanValue());
	Assert.assertTrue("idle, no watermark", watermarks.isEmpty());
}
 
Example #25
Source File: TestSplitWalDataLoss.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {
  final HRegionServer rs = testUtil.getRSForFirstRegionInTable(tableName);
  final HRegion region = (HRegion) rs.getRegions(tableName).get(0);
  HRegion spiedRegion = spy(region);
  final MutableBoolean flushed = new MutableBoolean(false);
  final MutableBoolean reported = new MutableBoolean(false);
  doAnswer(new Answer<FlushResult>() {
    @Override
    public FlushResult answer(InvocationOnMock invocation) throws Throwable {
      synchronized (flushed) {
        flushed.setValue(true);
        flushed.notifyAll();
      }
      synchronized (reported) {
        while (!reported.booleanValue()) {
          reported.wait();
        }
      }
      rs.getWAL(region.getRegionInfo()).abortCacheFlush(
        region.getRegionInfo().getEncodedNameAsBytes());
      throw new DroppedSnapshotException("testcase");
    }
  }).when(spiedRegion).internalFlushCacheAndCommit(Matchers.<WAL> any(),
    Matchers.<MonitoredTask> any(), Matchers.<PrepareFlushResult> any(),
    Matchers.<Collection<HStore>> any());
  // Find region key; don't pick up key for hbase:meta by mistake.
  String key = null;
  for (Map.Entry<String, HRegion> entry: rs.getOnlineRegions().entrySet()) {
    if (entry.getValue().getRegionInfo().getTable().equals(this.tableName)) {
      key = entry.getKey();
      break;
    }
  }
  rs.getOnlineRegions().put(key, spiedRegion);
  Connection conn = testUtil.getConnection();

  try (Table table = conn.getTable(tableName)) {
    table.put(new Put(Bytes.toBytes("row0"))
            .addColumn(family, qualifier, Bytes.toBytes("val0")));
  }
  long oldestSeqIdOfStore = region.getOldestSeqIdOfStore(family);
  LOG.info("CHANGE OLDEST " + oldestSeqIdOfStore);
  assertTrue(oldestSeqIdOfStore > HConstants.NO_SEQNUM);
  rs.getMemStoreFlusher().requestFlush(spiedRegion, FlushLifeCycleTracker.DUMMY);
  synchronized (flushed) {
    while (!flushed.booleanValue()) {
      flushed.wait();
    }
  }
  try (Table table = conn.getTable(tableName)) {
    table.put(new Put(Bytes.toBytes("row1"))
            .addColumn(family, qualifier, Bytes.toBytes("val1")));
  }
  long now = EnvironmentEdgeManager.currentTime();
  rs.tryRegionServerReport(now - 500, now);
  synchronized (reported) {
    reported.setValue(true);
    reported.notifyAll();
  }
  while (testUtil.getRSForFirstRegionInTable(tableName) == rs) {
    Thread.sleep(100);
  }
  try (Table table = conn.getTable(tableName)) {
    Result result = table.get(new Get(Bytes.toBytes("row0")));
    assertArrayEquals(Bytes.toBytes("val0"), result.getValue(family, qualifier));
  }
}
 
Example #26
Source File: SimpleDoneQueueManager.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public boolean addingFilter(QueryBundle<QUERY_TYPE, META_QUERY, MutableBoolean> queryBundle)
{
  return true;
}
 
Example #27
Source File: SimpleDoneQueueManager.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public void removedNode(QueueListNode<QueryBundle<QUERY_TYPE, META_QUERY, MutableBoolean>> queryQueueable)
{
  //Do nothing
}
 
Example #28
Source File: SimpleDoneQueueManager.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public void addedNode(QueueListNode<QueryBundle<QUERY_TYPE, META_QUERY, MutableBoolean>> queryQueueable)
{
  semaphore.release();
}
 
Example #29
Source File: SimpleDoneQueueManager.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public boolean removeBundle(QueryBundle<QUERY_TYPE, META_QUERY, MutableBoolean> queryQueueable)
{
  return queryQueueable.getQueueContext().booleanValue();
}
 
Example #30
Source File: ThresholdShedder.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public Multimap<String, String> findBundlesForUnloading(final LoadData loadData, final ServiceConfiguration conf) {
    selectedBundlesCache.clear();
    final double threshold = conf.getLoadBalancerBrokerThresholdShedderPercentage() / 100.0;
    final Map<String, Long> recentlyUnloadedBundles = loadData.getRecentlyUnloadedBundles();
    final double minThroughputThreshold = conf.getLoadBalancerBundleUnloadMinThroughputThreshold() * MB;

    final double avgUsage = getBrokerAvgUsage(loadData, conf.getLoadBalancerHistoryResourcePercentage(), conf);

    if (avgUsage == 0) {
        log.warn("average max resource usage is 0");
        return selectedBundlesCache;
    }

    loadData.getBrokerData().forEach((broker, brokerData) -> {
        final LocalBrokerData localData = brokerData.getLocalData();
        final double currentUsage = brokerAvgResourceUsage.getOrDefault(broker, 0.0);

        if (currentUsage < avgUsage + threshold) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] broker is not overloaded, ignoring at this point", broker);
            }
            return;
        }

        double percentOfTrafficToOffload = currentUsage - avgUsage - threshold + ADDITIONAL_THRESHOLD_PERCENT_MARGIN;
        double brokerCurrentThroughput = localData.getMsgThroughputIn() + localData.getMsgThroughputOut();
        double minimumThroughputToOffload = brokerCurrentThroughput * percentOfTrafficToOffload;

        if (minimumThroughputToOffload < minThroughputThreshold) {
            if (log.isDebugEnabled()) {
                log.info("[{}] broker is planning to shed throughput {} MByte/s less than " +
                                "minimumThroughputThreshold {} MByte/s, skipping bundle unload.",
                        broker, minimumThroughputToOffload / MB, minThroughputThreshold / MB);
            }
            return;
        }

        log.info(
                "Attempting to shed load on {}, which has max resource usage above avgUsage  and threshold {}%" +
                        " > {}% + {}% -- Offloading at least {} MByte/s of traffic, left throughput {} MByte/s",
                broker, currentUsage, avgUsage, threshold, minimumThroughputToOffload / MB,
                (brokerCurrentThroughput - minimumThroughputToOffload) / MB);

        MutableDouble trafficMarkedToOffload = new MutableDouble(0);
        MutableBoolean atLeastOneBundleSelected = new MutableBoolean(false);

        if (localData.getBundles().size() > 1) {
            loadData.getBundleData().entrySet().stream().map((e) -> {
                String bundle = e.getKey();
                BundleData bundleData = e.getValue();
                TimeAverageMessageData shortTermData = bundleData.getShortTermData();
                double throughput = shortTermData.getMsgThroughputIn() + shortTermData.getMsgThroughputOut();
                return Pair.of(bundle, throughput);
            }).filter(e ->
                    !recentlyUnloadedBundles.containsKey(e.getLeft())
            ).filter(e ->
                    localData.getBundles().contains(e.getLeft())
            ).sorted((e1, e2) ->
                    Double.compare(e2.getRight(), e1.getRight())
            ).forEach(e -> {
                if (trafficMarkedToOffload.doubleValue() < minimumThroughputToOffload
                        || atLeastOneBundleSelected.isFalse()) {
                    selectedBundlesCache.put(broker, e.getLeft());
                    trafficMarkedToOffload.add(e.getRight());
                    atLeastOneBundleSelected.setTrue();
                }
            });
        } else if (localData.getBundles().size() == 1) {
            log.warn(
                    "HIGH USAGE WARNING : Sole namespace bundle {} is overloading broker {}. " +
                            "No Load Shadding will be done on this broker",
                    localData.getBundles().iterator().next(), broker);
        } else {
            log.warn("Broker {} is overloaded despit having no bundles", broker);
        }
    });

    return selectedBundlesCache;
}