@Override public void enable() { if (_isPrimary()) { return; } _enable(); }
@Override public void force() { if (_isPrimary()) { return; } _force(); }
private void disableNow() { if (_isPrimary()) { _disable(); } doStopUponCycleComplete = false; wasPrimary = false; }
@Test public void testEnabledDisabledCyle() { BasicSingleProducerEnforcer se = new BasicSingleProducerEnforcer(); Assert.assertTrue(se.isPrimary()); se.onCycleStart(1234L); se.onCycleComplete(null, 10L, TimeUnit.SECONDS); se.disable(); Assert.assertFalse(se.isPrimary()); }
/** * Invoke this method to alter runCycle behavior. If this Producer is not primary, runCycle is a no-op. Note that by default, * SingleProducerEnforcer is instantiated as BasicSingleProducerEnforcer, which is initialized to return true for isPrimary() * * @param doEnable true if enable primary producer, if false * @return true if the intended action was successful */ public boolean enablePrimaryProducer(boolean doEnable) { if (doEnable) { singleProducerEnforcer.enable(); } else { singleProducerEnforcer.disable(); } return (singleProducerEnforcer.isPrimary() == doEnable); }
@Test public void testNotPrimaryProducerVersion() { BasicSingleProducerEnforcer enforcer = new BasicSingleProducerEnforcer(); HollowProducer producer = HollowProducer.withPublisher(new FakeBlobPublisher()) .withSingleProducerEnforcer(enforcer) .withAnnouncer(new HollowFilesystemAnnouncer(tmpFolder.toPath())) .build(); producer.addListener(new FakeProducerListener()); long v1 = producer.runCycle(ws -> { ws.add(1); }); enforcer.disable(); // Run cycle as not the primary producer long v2 = producer.runCycle(ws -> { ws.add(1); }); // Run cycle as the primary producer enforcer.enable(); long v3 = producer.runCycle(ws -> { ws.add(2); }); Assert.assertEquals(v1, v2); Assert.assertTrue(v3 > v2); }
@Test public void testCycleStartEnd() { Mockito.when(singleProducerEnforcer.isPrimary()).thenReturn(true); this.producer.runCycle(Mockito.mock(HollowProducer.Populator.class)); Mockito.verify(listener).onCycleStart(ArgumentMatchers.anyLong()); Mockito.verify(listener).onCycleComplete( Mockito.any(HollowProducerListener.ProducerStatus.class), ArgumentMatchers.anyLong(), Mockito.any(TimeUnit.class)); } }
@Deprecated public HollowProducer( Publisher publisher, Announcer announcer) { this(new HollowFilesystemBlobStager(), publisher, announcer, Collections.emptyList(), new VersionMinterWithCounter(), null, 0, DEFAULT_TARGET_MAX_TYPE_SHARD_SIZE, null, new DummyBlobStorageCleaner(), new BasicSingleProducerEnforcer()); }
@Override public void disable() { if (!hasCycleStarted) { disableNow(); } else { doStopUponCycleComplete = true; } }
@Override public boolean isPrimary() { final boolean primary = _isPrimary(); if (!primary) { if (wasPrimary) { logger.log(Level.WARNING, "SingleProducerEnforcer: lost primary producer status"); } } else { wasPrimary = true; } return primary; }
@Test public void testMultiCycle() { BasicSingleProducerEnforcer se = new BasicSingleProducerEnforcer(); for (int i = 0; i < 10; i++) { se.enable(); Assert.assertTrue(se.isPrimary()); se.onCycleStart(1234L); se.disable(); Assert.assertTrue(se.isPrimary()); se.onCycleComplete(null, 10L, TimeUnit.SECONDS); Assert.assertFalse(se.isPrimary()); } }
@Test public void testCycleSkip() { Mockito.when(singleProducerEnforcer.isPrimary()).thenReturn(false); this.producer.runCycle(null); Mockito.verify(listener).onCycleSkip( HollowProducerListener.CycleSkipReason.NOT_PRIMARY_PRODUCER); Mockito.verify(listener, Mockito.never()).onCycleStart( ArgumentMatchers.anyLong()); }
@Override public void onCycleComplete(ProducerStatus status, long elapsed, TimeUnit unit) { hasCycleStarted = false; if (doStopUponCycleComplete) { disableNow(); } }
@Test public void testTransitions() { BasicSingleProducerEnforcer se = new BasicSingleProducerEnforcer(); Assert.assertTrue(se.isPrimary()); se.onCycleStart(1234L); Assert.assertTrue(se.isPrimary()); se.disable(); Assert.assertTrue(se.isPrimary()); se.onCycleComplete(null, 10L, TimeUnit.SECONDS); Assert.assertFalse(se.isPrimary()); se.enable(); Assert.assertTrue(se.isPrimary()); se.onCycleStart(1235L); Assert.assertTrue(se.isPrimary()); se.disable(); Assert.assertTrue(se.isPrimary()); se.onCycleComplete(null, 10L, TimeUnit.SECONDS); Assert.assertFalse(se.isPrimary()); }
ListenerSupport.Listeners localListeners = listeners.listeners(); if (!singleProducerEnforcer.isPrimary()) {