Search in sources :

Example 6 with PersistentGlobalState

use of com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState in project nrtsearch by Yelp.

the class LocalStateBackendTest method testCommitGlobalState.

@Test
public void testCommitGlobalState() throws IOException {
    StateBackend stateBackend = new LocalStateBackend(getMockGlobalState());
    Path filePath = getStateFilePath();
    PersistentGlobalState initialState = stateBackend.loadOrCreateGlobalState();
    Map<String, IndexInfo> indicesMap = new HashMap<>();
    indicesMap.put("test_index", new IndexInfo());
    indicesMap.put("test_index_2", new IndexInfo());
    PersistentGlobalState updatedState = new PersistentGlobalState(indicesMap);
    assertNotEquals(initialState, updatedState);
    stateBackend.commitGlobalState(updatedState);
    PersistentGlobalState loadedState = StateUtils.readStateFromFile(filePath);
    assertEquals(updatedState, loadedState);
    indicesMap = new HashMap<>();
    indicesMap.put("test_index_3", new IndexInfo());
    PersistentGlobalState updatedState2 = new PersistentGlobalState(indicesMap);
    assertNotEquals(updatedState, updatedState2);
    stateBackend.commitGlobalState(updatedState2);
    loadedState = StateUtils.readStateFromFile(filePath);
    assertEquals(updatedState2, loadedState);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) PersistentGlobalState(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState) IndexInfo(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState.IndexInfo) Test(org.junit.Test)

Example 7 with PersistentGlobalState

use of com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState in project nrtsearch by Yelp.

the class RemoteStateBackendTest method getS3State.

private PersistentGlobalState getS3State() throws IOException {
    long currentVersion = versionManager.getLatestVersionNumber(TEST_SERVICE_NAME, RemoteStateBackend.GLOBAL_STATE_RESOURCE);
    if (currentVersion < 0) {
        return null;
    }
    String versionHash = versionManager.getVersionString(TEST_SERVICE_NAME, RemoteStateBackend.GLOBAL_STATE_RESOURCE, String.valueOf(currentVersion));
    Path s3FilePath = getS3FilePath(versionHash);
    assertTrue(s3FilePath.toFile().exists());
    assertTrue(s3FilePath.toFile().isFile());
    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new LZ4FrameInputStream(new FileInputStream(s3FilePath.toFile())));
    PersistentGlobalState stateFromTar = null;
    for (TarArchiveEntry tarArchiveEntry = tarArchiveInputStream.getNextTarEntry(); tarArchiveEntry != null; tarArchiveEntry = tarArchiveInputStream.getNextTarEntry()) {
        if (tarArchiveEntry.getName().endsWith(StateUtils.GLOBAL_STATE_FILE)) {
            String stateStr;
            try (DataInputStream dataInputStream = new DataInputStream(tarArchiveInputStream)) {
                stateStr = dataInputStream.readUTF();
            }
            stateFromTar = StateUtils.MAPPER.readValue(stateStr, PersistentGlobalState.class);
            break;
        }
    }
    return stateFromTar;
}
Also used : Path(java.nio.file.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) PersistentGlobalState(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState) DataInputStream(java.io.DataInputStream) LZ4FrameInputStream(net.jpountz.lz4.LZ4FrameInputStream) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 8 with PersistentGlobalState

use of com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState in project nrtsearch by Yelp.

the class RemoteStateBackendTest method testCommitGlobalState.

@Test
public void testCommitGlobalState() throws IOException {
    StateBackend stateBackend = new RemoteStateBackend(getMockGlobalState(false));
    Path localFilePath = getLocalStateFilePath();
    PersistentGlobalState initialState = stateBackend.loadOrCreateGlobalState();
    Map<String, IndexInfo> indicesMap = new HashMap<>();
    indicesMap.put("test_index", new IndexInfo());
    indicesMap.put("test_index_2", new IndexInfo());
    PersistentGlobalState updatedState = new PersistentGlobalState(indicesMap);
    assertNotEquals(initialState, updatedState);
    stateBackend.commitGlobalState(updatedState);
    PersistentGlobalState loadedState = getS3State();
    assertEquals(updatedState, loadedState);
    PersistentGlobalState loadedLocalState = StateUtils.readStateFromFile(localFilePath);
    assertEquals(updatedState, loadedLocalState);
    indicesMap = new HashMap<>();
    indicesMap.put("test_index_3", new IndexInfo());
    PersistentGlobalState updatedState2 = new PersistentGlobalState(indicesMap);
    assertNotEquals(updatedState, updatedState2);
    stateBackend.commitGlobalState(updatedState2);
    loadedState = getS3State();
    assertEquals(updatedState2, loadedState);
    loadedLocalState = StateUtils.readStateFromFile(localFilePath);
    assertEquals(updatedState2, loadedLocalState);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) PersistentGlobalState(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState) IndexInfo(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState.IndexInfo) Test(org.junit.Test)

Example 9 with PersistentGlobalState

use of com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState in project nrtsearch by Yelp.

the class RemoteStateBackendTest method testReadOnlyWithInitialState.

@Test
public void testReadOnlyWithInitialState() throws IOException {
    StateBackend stateBackend = new RemoteStateBackend(getMockGlobalState(true));
    Map<String, IndexInfo> indicesMap = new HashMap<>();
    indicesMap.put("test_index", new IndexInfo());
    indicesMap.put("test_index_2", new IndexInfo());
    PersistentGlobalState initialState = new PersistentGlobalState(indicesMap);
    writeStateToS3(initialState);
    PersistentGlobalState loadedState = stateBackend.loadOrCreateGlobalState();
    assertEquals(initialState, loadedState);
}
Also used : HashMap(java.util.HashMap) PersistentGlobalState(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState) IndexInfo(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState.IndexInfo) Test(org.junit.Test)

Example 10 with PersistentGlobalState

use of com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState in project nrtsearch by Yelp.

the class RemoteStateBackendTest method testCreatesDefaultState.

@Test
public void testCreatesDefaultState() throws IOException {
    StateBackend stateBackend = new RemoteStateBackend(getMockGlobalState(false));
    Path localFilePath = getLocalStateFilePath();
    assertFalse(localFilePath.toFile().exists());
    assertNull(getS3State());
    PersistentGlobalState globalState = stateBackend.loadOrCreateGlobalState();
    assertEquals(globalState, new PersistentGlobalState());
    assertTrue(localFilePath.toFile().exists());
    assertTrue(localFilePath.toFile().isFile());
    PersistentGlobalState loadedLocalState = StateUtils.readStateFromFile(localFilePath);
    assertEquals(globalState, loadedLocalState);
    PersistentGlobalState stateFromTar = getS3State();
    assertNotNull(stateFromTar);
    assertEquals(globalState, stateFromTar);
}
Also used : Path(java.nio.file.Path) PersistentGlobalState(com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState) Test(org.junit.Test)

Aggregations

PersistentGlobalState (com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState)11 Path (java.nio.file.Path)9 Test (org.junit.Test)8 IndexInfo (com.yelp.nrtsearch.server.luceneserver.state.PersistentGlobalState.IndexInfo)6 HashMap (java.util.HashMap)6 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 LZ4FrameInputStream (net.jpountz.lz4.LZ4FrameInputStream)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1