Java Code Examples for java.util.concurrent.Semaphore#drainPermits()

The following examples show how to use java.util.concurrent.Semaphore#drainPermits() . 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: ClassicDoomSoundDriver.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
public ClassicDoomSoundDriver(DoomMain<?,?> DM, int numChannels) {
	super(DM,numChannels);
    channelleftvol_lookup = new int[numChannels][];
    channelrightvol_lookup = new int[numChannels][];
    channelstep = new int[numChannels];
    channelstepremainder = new int[numChannels];
    channels = new byte[numChannels][];
    p_channels = new int[numChannels];
    channelsend = new int[numChannels];
    produce = new Semaphore(1);
    consume = new Semaphore(1);
    produce.drainPermits();
    mixbuffer= new byte[MIXBUFFERSIZE];
}
 
Example 2
Source File: SuperDoomSoundDriver.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
public SuperDoomSoundDriver(DoomMain<?, ?> DM, int numChannels) {
	super(DM,numChannels);
    channels = new boolean[numChannels];
    produce = new Semaphore(1);
    consume = new Semaphore(1);
    update_mixer = new Semaphore(1);
    produce.drainPermits();
    update_mixer.drainPermits();
    this.MIXSRV=new MixServer(numChannels);
    MIXTIMER= new Timer(true);
    // Sound tics every 1/35th of a second. Grossly
    // inaccurate under Windows though, will get rounded
    // down to the closest multiple of 15 or 16 ms.
    MIXTIMER.schedule(new SoundTimer(), 0,SOUND_PERIOD);        
}
 
Example 3
Source File: TestExhibitorEnsembleProvider.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testExhibitorFailures() throws Exception
{
    final AtomicReference<String>   backupConnectionString = new AtomicReference<String>("backup1:1");
    final AtomicReference<String>   connectionString = new AtomicReference<String>("count=1&port=2&server0=localhost");
    Exhibitors                      exhibitors = new Exhibitors
    (
        Lists.newArrayList("foo", "bar"),
        1000,
        new Exhibitors.BackupConnectionStringProvider()
        {
            @Override
            public String getBackupConnectionString()
            {
                return backupConnectionString.get();
            }
        }
    );
    ExhibitorRestClient             mockRestClient = new ExhibitorRestClient()
    {
        @Override
        public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception
        {
            String localConnectionString = connectionString.get();
            if ( localConnectionString == null )
            {
                throw new IOException();
            }
            return localConnectionString;
        }
    };

    final Semaphore             semaphore = new Semaphore(0);
    ExhibitorEnsembleProvider   provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1))
    {
        @Override
        protected void poll()
        {
            super.poll();
            semaphore.release();
        }
    };
    provider.pollForInitialEnsemble();
    try
    {
        provider.start();

        Assert.assertEquals(provider.getConnectionString(), "localhost:2");

        connectionString.set(null);
        semaphore.drainPermits();
        semaphore.acquire();    // wait for next poll
        Assert.assertEquals(provider.getConnectionString(), "backup1:1");

        backupConnectionString.set("backup2:2");
        semaphore.drainPermits();
        semaphore.acquire();    // wait for next poll
        Assert.assertEquals(provider.getConnectionString(), "backup2:2");

        connectionString.set("count=1&port=3&server0=localhost3");
        semaphore.drainPermits();
        semaphore.acquire();    // wait for next poll
        Assert.assertEquals(provider.getConnectionString(), "localhost3:3");
    }
    finally
    {
        CloseableUtils.closeQuietly(provider);
    }
}
 
Example 4
Source File: TestExhibitorEnsembleProvider.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testChanging() throws Exception
{
    TestingServer               secondServer = new TestingServer();
    try
    {
        String                          mainConnectionString = "count=1&port=" + server.getPort() + "&server0=localhost";
        String                          secondConnectionString = "count=1&port=" + secondServer.getPort() + "&server0=localhost";

        final Semaphore                 semaphore = new Semaphore(0);
        final AtomicReference<String>   connectionString = new AtomicReference<String>(mainConnectionString);
        Exhibitors                      exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
        ExhibitorRestClient             mockRestClient = new ExhibitorRestClient()
        {
            @Override
            public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception
            {
                semaphore.release();
                return connectionString.get();
            }
        };
        ExhibitorEnsembleProvider   provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
        provider.pollForInitialEnsemble();

        Timing                          timing = new Timing().multiple(4);
        final CuratorZookeeperClient    client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new RetryOneTime(2));
        client.start();
        try
        {
            RetryLoop.callWithRetry
            (
                client,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        client.getZooKeeper().create("/test", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                        return null;
                    }
                }
            );

            connectionString.set(secondConnectionString);
            semaphore.drainPermits();
            semaphore.acquire();

            server.stop();  // create situation where the current zookeeper gets a sys-disconnected

            Stat        stat = RetryLoop.callWithRetry
            (
                client,
                new Callable<Stat>()
                {
                    @Override
                    public Stat call() throws Exception
                    {
                        return client.getZooKeeper().exists("/test", false);
                    }
                }
            );
            Assert.assertNull(stat);    // it's a different server so should be null
        }
        finally
        {
            client.close();
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(secondServer);
    }
}
 
Example 5
Source File: IndexerTransactionTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Checks that added files are not visible until their source root is added,
 * even if the memcache with file contents 
 * is flushed during the scan (will be flushed after each file)
 */
public void testAddedClassesNotVisible() throws Exception {
    commonSetup();
    final Semaphore signal = new Semaphore(0);

    logHandler.beforeFinishCallback = new RootScannedCallback("java", "src/", signal);
    
    Set<ElementHandle<TypeElement>> handles;

    handles = ci.getDeclaredTypes("ConstructorTest", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    // copy over some files
    TestUtil.copyFiles(
            new File(getDataDir(), "indexing/files"),
            new File(FileUtil.toFile(srcRoot), "org/netbeans/parsing/source1".replace('/', File.separatorChar)),
                    "ConstructorTest.java", "EmptyClass.java");

    // must force rescan, indexer does not notice the file-copy ?
    RepositoryUpdater.getDefault().refreshAll(false, false, true, null, 
            srcRoot, srcRoot2);
    try {
        signal.acquire();
        signal.drainPermits();
    } catch (InterruptedException ex) {
        fail("Should rescan the added files");
    }

    handles = ci.getDeclaredTypes("ConstructorTest", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    assertEquals(0, handles.size());    
    
    // check that files STILL do not exist
    File dir = findSegmentDir(srcRoot);
    File targetDir = new File(dir, "org/netbeans/parsing/source1".replace('/', File.separatorChar));
    
    assertFalse(new File(targetDir, "ConstructorTest.sig").exists());
    assertFalse(new File(targetDir, "EmptyClass.sig").exists());
    
    parserBlocker.release();
    
    logHandler.waitForInitialScan();
    
    handles = ci.getDeclaredTypes("ConstructorTest", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    assertEquals(1, handles.size());    

    assertTrue(new File(targetDir, "ConstructorTest.sig").exists());
    assertTrue(new File(targetDir, "EmptyClass.sig").exists());
}
 
Example 6
Source File: IndexerTransactionTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Checks that deleted classes remain visible until the source root is committed.
 * 
 * @throws Exception 
 */
public void testDeletedClassesVisible() throws Exception {
    commonSetup();
    final Semaphore signal = new Semaphore(0);

    logHandler.beforeFinishCallback = new RootScannedCallback("java", "src/", signal);
    
    Set<ElementHandle<TypeElement>> handles;

    handles = ci.getDeclaredTypes("ClassWithInnerClass", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    
    assertEquals(1, handles.size());
    
    File sourceDir = new File(FileUtil.toFile(srcRoot), "org/netbeans/parsing/source1".replace('/', File.separatorChar));
    new File(sourceDir, "ClassWithInnerClass.java").delete();
    
    // must force rescan, indexer does not notice the file-copy ?
    RepositoryUpdater.getDefault().refreshAll(false, false, true, null, 
            srcRoot, srcRoot2);
    try {
        signal.acquire();
        signal.drainPermits();
    } catch (InterruptedException ex) {
        fail("Should rescan the added files");
    }

    handles = ci.getDeclaredTypes("ClassWithInnerClass", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    assertEquals(1, handles.size());    
    
    // check that files STILL do not exist
    File dir = findSegmentDir(srcRoot);
    File targetDir = new File(dir, "org/netbeans/parsing/source1".replace('/', File.separatorChar));
    
    assertTrue(new File(targetDir, "ClassWithInnerClass.sig").exists());
    
    parserBlocker.release();
    
    logHandler.waitForInitialScan();
    
    handles = ci.getDeclaredTypes("ClassWithInnerClass", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    assertEquals(0, handles.size());    

    assertFalse(new File(targetDir, "ClassWithInnerClass.sig").exists());
}
 
Example 7
Source File: ModelUnitTest.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
/** Check PV changes, using local PV */
@Test
public void testSaveRestore() throws Exception
{
    // Local PV that's also used in one of the cells
    final PV pv = PVPool.getPV("loc://setpoint1(1)");
    // Change value from initial 1 to 3.14
    pv.write(3.14);

    final Semaphore have_pv_update = new Semaphore(0);
    final Disposable subscription = pv.onValueEvent().subscribe(value ->
    {
        System.out.println("PV: " + VTypeHelper.toString(value));
        have_pv_update.release();
    });

    // Start model
    final Model model = new Model(Model.class.getResourceAsStream("/pace_examples/localtest.pace"));
    model.addListener(listener);
    assertThat(model.getInstances().get(0).getCell(0).getName(), equalTo("loc://setpoint1(1)"));
    model.start();

    // Model should reflect the current value of the PV in one of its cells
    for (int timeout=0; timeout<10; ++timeout)
    {
        if ("3.14".equals(model.getInstances().get(0).getCell(0).getCurrentValue()))
            break;
        Thread.sleep(200);
    }
    assertFalse(model.isEdited());
    assertThat(model.getInstances().get(0).getCell(0).getCurrentValue(), equalTo("3.14"));

    // Simulate user-entered value
    model.getInstances().get(0).getCell(0).setUserValue("6.28");
    assertTrue(model.isEdited());
    // PV and original value are unchanged, but cell shows the user data
    assertThat(VTypeHelper.toString(pv.read()), equalTo("3.14"));
    assertThat(model.getInstances().get(0).getCell(0).getCurrentValue(), equalTo("3.14"));
    assertThat(model.getInstances().get(0).getCell(0).getObservable().getValue(), equalTo("6.28"));

    // Write model to PVs
    have_pv_update.drainPermits();
    model.saveUserValues("test");
    assertTrue(have_pv_update.tryAcquire(2, TimeUnit.SECONDS));
    assertThat(VTypeHelper.toString(pv.read()), equalTo("6.28"));
    // Model is still 'edited' because we didn't revert nor clear
    assertTrue(model.isEdited());


    // Revert
    have_pv_update.drainPermits();
    model.revertOriginalValues();
    assertTrue(have_pv_update.tryAcquire(2, TimeUnit.SECONDS));
    assertThat(VTypeHelper.toString(pv.read()), equalTo("3.14"));

    // Reverting PVs to the original values means
    // the model receives these updates,
    // and is then back in a dirty state with user-entered values
    // that have not been written.
    // These updates, however, take some time because of throttling
    for (int timeout=0; timeout<10; ++timeout)
    {
        if (model.isEdited())
            break;
        Thread.sleep(300);
    }
    assertTrue(model.isEdited());

    model.clearUserValues();
    assertFalse(model.isEdited());

    // Simulate user-entered value
    model.getInstances().get(0).getCell(0).setUserValue("10.0");
    assertTrue(model.isEdited());

    // Write model to PVs, submit
    assertThat(VTypeHelper.toString(pv.read()), equalTo("3.14"));
    model.saveUserValues("test");
    model.clearUserValues();
    assertFalse(model.isEdited());
    assertTrue(have_pv_update.tryAcquire(2, TimeUnit.SECONDS));
    assertThat(VTypeHelper.toString(pv.read()), equalTo("10.0"));

    model.stop();

    subscription.dispose();
    PVPool.releasePV(pv);
}
 
Example 8
Source File: CdcrUpdateLogTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Check that the reader is correctly reset to its last position
 */
@Test
public void testGetNumberOfRemainingRecords() throws Exception {
  try {
    TestInjection.skipIndexWriterCommitOnClose = true;
    final Semaphore logReplayFinish = new Semaphore(0);
    UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release();

    this.clearCore();

    int start = 0;

    LinkedList<Long> versions = new LinkedList<>();
    addDocs(10, start, versions);
    start += 10;
    assertU(commit());

    addDocs(10, start, versions);
    start += 10;

    h.close();
    logReplayFinish.drainPermits();
    createCore();

    // At this stage, we have re-opened a capped tlog, and an uncapped tlog.
    // check that the number of remaining records is correctly computed in these two cases

    CdcrUpdateLog ulog = (CdcrUpdateLog) h.getCore().getUpdateHandler().getUpdateLog();
    CdcrUpdateLog.CdcrLogReader reader = ulog.newLogReader();

    // wait for the replay to finish
    assertTrue(logReplayFinish.tryAcquire(timeout, TimeUnit.SECONDS));

    // 20 records + 2 commits
    assertEquals(22, reader.getNumberOfRemainingRecords());

    for (int i = 0; i < 22; i++) {
      Object o = reader.next();
      assertNotNull(o);
      assertEquals(22 - (i + 1), reader.getNumberOfRemainingRecords());
    }
    assertNull(reader.next());
    assertEquals(0, reader.getNumberOfRemainingRecords());

    // It should pick up the new tlog files
    addDocs(10, start, versions);
    assertEquals(10, reader.getNumberOfRemainingRecords());
  } finally {
    TestInjection.skipIndexWriterCommitOnClose = false; // reset
    UpdateLog.testing_logReplayFinishHook = null;
  }
}
 
Example 9
Source File: TestRecovery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testTruncatedLog() throws Exception {
  try {
    TestInjection.skipIndexWriterCommitOnClose = true;
    final Semaphore logReplay = new Semaphore(0);
    final Semaphore logReplayFinish = new Semaphore(0);

    UpdateLog.testing_logReplayHook = () -> {
      try {
        assertTrue(logReplay.tryAcquire(timeout, TimeUnit.SECONDS));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    };

    UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release();

    UpdateLog ulog = h.getCore().getUpdateHandler().getUpdateLog();
    File logDir = new File(h.getCore().getUpdateHandler().getUpdateLog().getLogDir());

    clearIndex();
    assertU(commit());

    assertU(adoc("id","F1"));
    assertU(adoc("id","F2"));
    assertU(adoc("id","F3"));

    h.close();
    String[] files = ulog.getLogList(logDir);
    Arrays.sort(files);
    try (RandomAccessFile raf = new RandomAccessFile(new File(logDir, files[files.length - 1]), "rw")) {
      raf.seek(raf.length());  // seek to end
      raf.writeLong(0xffffffffffffffffL);
      raf.writeChars("This should be appended to a good log file, representing a bad partially written record.");
    }

    logReplay.release(1000);
    logReplayFinish.drainPermits();
    ignoreException("OutOfBoundsException");  // this is what the corrupted log currently produces... subject to change.
    createCore();
    assertTrue(logReplayFinish.tryAcquire(timeout, TimeUnit.SECONDS));
    resetExceptionIgnores();
    assertJQ(req("q","*:*") ,"/response/numFound==3");

    //
    // Now test that the bad log file doesn't mess up retrieving latest versions
    //

    String v104 = getNextVersion();
    String v105 = getNextVersion();
    String v106 = getNextVersion();
    
    updateJ(jsonAdd(sdoc("id","F4", "_version_",v104)), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
    updateJ(jsonAdd(sdoc("id","F5", "_version_",v105)), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
    updateJ(jsonAdd(sdoc("id","F6", "_version_",v106)), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));

    // This currently skips the bad log file and also returns the version of the clearIndex (del *:*)
    // assertJQ(req("qt","/get", "getVersions","6"), "/versions==[106,105,104]");
    assertJQ(req("qt","/get", "getVersions","3"), "/versions==["+v106+","+v105+","+v104+"]");

  } finally {
    UpdateLog.testing_logReplayHook = null;
    UpdateLog.testing_logReplayFinishHook = null;
  }
}
 
Example 10
Source File: TestRecovery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoveryMultipleLogs() throws Exception {
  try {
    TestInjection.skipIndexWriterCommitOnClose = true;
    final Semaphore logReplay = new Semaphore(0);
    final Semaphore logReplayFinish = new Semaphore(0);

    UpdateLog.testing_logReplayHook = () -> {
      try {
        assertTrue(logReplay.tryAcquire(timeout, TimeUnit.SECONDS));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    };

    UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release();

    UpdateLog ulog = h.getCore().getUpdateHandler().getUpdateLog();
    File logDir = new File(h.getCore().getUpdateHandler().getUpdateLog().getLogDir());

    clearIndex();
    assertU(commit());

    assertU(adoc("id","AAAAAA"));
    assertU(adoc("id","BBBBBB"));
    assertU(adoc("id","CCCCCC"));

    h.close();
    String[] files = ulog.getLogList(logDir);
    Arrays.sort(files);
    String fname = files[files.length-1];
    byte[] content;
    try (RandomAccessFile raf = new RandomAccessFile(new File(logDir, fname), "rw")) {
      raf.seek(raf.length());  // seek to end
      raf.writeLong(0xffffffffffffffffL);
      raf.writeChars("This should be appended to a good log file, representing a bad partially written record.");

      content = new byte[(int) raf.length()];
      raf.seek(0);
      raf.readFully(content);
    }

    // Now make a newer log file with just the IDs changed.  NOTE: this may not work if log format changes too much!
    findReplace("AAAAAA".getBytes(StandardCharsets.UTF_8), "aaaaaa".getBytes(StandardCharsets.UTF_8), content);
    findReplace("BBBBBB".getBytes(StandardCharsets.UTF_8), "bbbbbb".getBytes(StandardCharsets.UTF_8), content);
    findReplace("CCCCCC".getBytes(StandardCharsets.UTF_8), "cccccc".getBytes(StandardCharsets.UTF_8), content);

    // WARNING... assumes format of .00000n where n is less than 9
    long logNumber = Long.parseLong(fname.substring(fname.lastIndexOf(".") + 1));
    String fname2 = String.format(Locale.ROOT,
        UpdateLog.LOG_FILENAME_PATTERN,
        UpdateLog.TLOG_NAME,
        logNumber + 1);
    try (RandomAccessFile raf = new RandomAccessFile(new File(logDir, fname2), "rw")) {
      raf.write(content);
    }

    logReplay.release(1000);
    logReplayFinish.drainPermits();
    ignoreException("OutOfBoundsException");  // this is what the corrupted log currently produces... subject to change.
    createCore();
    assertTrue(logReplayFinish.tryAcquire(timeout, TimeUnit.SECONDS));
    resetExceptionIgnores();
    assertJQ(req("q","*:*") ,"/response/numFound==6");

  } finally {
    UpdateLog.testing_logReplayHook = null;
    UpdateLog.testing_logReplayFinishHook = null;
  }
}
 
Example 11
Source File: TestRecoveryHdfs.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testTruncatedLog() throws Exception {
  try {
    TestInjection.skipIndexWriterCommitOnClose = true;
    final Semaphore logReplay = new Semaphore(0);
    final Semaphore logReplayFinish = new Semaphore(0);

    UpdateLog.testing_logReplayHook = () -> {
      try {
        assertTrue(logReplay.tryAcquire(TIMEOUT, TimeUnit.SECONDS));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    };

    UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release();

    String logDir = h.getCore().getUpdateHandler().getUpdateLog().getLogDir();

    clearIndex();
    assertU(commit());

    assertU(adoc("id","F1"));
    assertU(adoc("id","F2"));
    assertU(adoc("id","F3"));
    
    h.close();
    

    
    String[] files = HdfsUpdateLog.getLogList(fs, new Path(logDir));
    Arrays.sort(files);

    FSDataOutputStream dos = fs.append(new Path(logDir, files[files.length-1]));
  
    dos.writeLong(0xffffffffffffffffL);
    dos.writeChars("This should be appended to a good log file, representing a bad partially written record.");
    dos.close();

    logReplay.release(1000);
    logReplayFinish.drainPermits();
    ignoreException("OutOfBoundsException");  // this is what the corrupted log currently produces... subject to change.
    createCore();
    assertTrue(logReplayFinish.tryAcquire(TIMEOUT, TimeUnit.SECONDS));
    resetExceptionIgnores();
    assertJQ(req("q","*:*") ,"/response/numFound==3");

    //
    // Now test that the bad log file doesn't mess up retrieving latest versions
    //

    updateJ(jsonAdd(sdoc("id","F4", "_version_","104")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
    updateJ(jsonAdd(sdoc("id","F5", "_version_","105")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));
    updateJ(jsonAdd(sdoc("id","F6", "_version_","106")), params(DISTRIB_UPDATE_PARAM,FROM_LEADER));

    // This currently skips the bad log file and also returns the version of the clearIndex (del *:*)
    // assertJQ(req("qt","/get", "getVersions","6"), "/versions==[106,105,104]");
    assertJQ(req("qt","/get", "getVersions","3"), "/versions==[106,105,104]");

  } finally {
    UpdateLog.testing_logReplayHook = null;
    UpdateLog.testing_logReplayFinishHook = null;
  }
}
 
Example 12
Source File: TestRecoveryHdfs.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoveryMultipleLogs() throws Exception {
  try {
    TestInjection.skipIndexWriterCommitOnClose = true;
    final Semaphore logReplay = new Semaphore(0);
    final Semaphore logReplayFinish = new Semaphore(0);

    UpdateLog.testing_logReplayHook = () -> {
      try {
        assertTrue(logReplay.tryAcquire(TIMEOUT, TimeUnit.SECONDS));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    };

    UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release();

    String logDir = h.getCore().getUpdateHandler().getUpdateLog().getLogDir();

    clearIndex();
    assertU(commit());

    assertU(adoc("id","AAAAAA"));
    assertU(adoc("id","BBBBBB"));
    assertU(adoc("id","CCCCCC"));

    h.close();
    String[] files = HdfsUpdateLog.getLogList(fs, new Path(logDir));
    Arrays.sort(files);
    String fname = files[files.length-1];

    try(FSDataOutputStream dos = fs.append(new Path(logDir, files[files.length-1]))) {
      dos.writeLong(0xffffffffffffffffL);
      dos.writeChars("This should be appended to a good log file, representing a bad partially written record.");
    }

    try(FSDataInputStream dis = fs.open(new Path(logDir, files[files.length-1]))) {
      byte[] content = new byte[dis.available()];
      dis.readFully(content);

      // Now make a newer log file with just the IDs changed.  NOTE: this may not work if log format changes too much!
      findReplace("AAAAAA".getBytes(StandardCharsets.UTF_8), "aaaaaa".getBytes(StandardCharsets.UTF_8), content);
      findReplace("BBBBBB".getBytes(StandardCharsets.UTF_8), "bbbbbb".getBytes(StandardCharsets.UTF_8), content);
      findReplace("CCCCCC".getBytes(StandardCharsets.UTF_8), "cccccc".getBytes(StandardCharsets.UTF_8), content);

      // WARNING... assumes format of .00000n where n is less than 9
      long logNumber = Long.parseLong(fname.substring(fname.lastIndexOf(".") + 1));
      String fname2 = String.format(Locale.ROOT,
          UpdateLog.LOG_FILENAME_PATTERN,
          UpdateLog.TLOG_NAME,
          logNumber + 1);

      try(FSDataOutputStream dos = fs.create(new Path(logDir, fname2), (short)1)) {
        dos.write(content);
      }
    }

    logReplay.release(1000);
    logReplayFinish.drainPermits();
    ignoreException("OutOfBoundsException");  // this is what the corrupted log currently produces... subject to change.
    createCore();
    assertTrue(logReplayFinish.tryAcquire(TIMEOUT, TimeUnit.SECONDS));
    resetExceptionIgnores();
    assertJQ(req("q","*:*") ,"/response/numFound==6");

  } finally {
    UpdateLog.testing_logReplayHook = null;
    UpdateLog.testing_logReplayFinishHook = null;
  }
}
 
Example 13
Source File: DefaultCommandStoreTest.java    From x-pipe with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetAsSoonAsMessageWritten() throws IOException, InterruptedException {

	final StringBuilder sb = new StringBuilder();
	final Semaphore semaphore = new Semaphore(0);

	executors.execute(new AbstractExceptionLogTask() {

		@Override
		protected void doRun() throws Exception {
			commandStore.addCommandsListener(0, new CommandsListener() {

				@Override
				public ChannelFuture onCommand(ReferenceFileRegion referenceFileRegion) {

					sb.append(readFileChannelInfoMessageAsString(referenceFileRegion));
					semaphore.release();
					return null;
				}

				@Override
				public boolean isOpen() {
					return true;
				}

				@Override
				public void beforeCommand() {

				}
			});
		}
	});

	StringBuilder expected = new StringBuilder();
	for (int i = 0; i < (1 << 10); i++) {

		byte random = (byte) randomInt('a', 'z');
		semaphore.drainPermits();
		expected.append((char) random);
		commandStore.appendCommands(Unpooled.wrappedBuffer(new byte[] { random }));

		Assert.assertTrue(semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
		logger.debug("{}", sb);
		Assert.assertEquals(expected.toString(), sb.toString());
	}

}