Search in sources :

Example 6 with SymbolTable

use of com.linkedin.data.codec.symbol.SymbolTable in project rest.li by linkedin.

the class TestRestLiSymbolTableProvider method testGetValidLocalSymbolTable.

@Test
public void testGetValidLocalSymbolTable() {
    _provider.onInitialized(Collections.unmodifiableMap(Collections.singletonMap("TestResourceName", _resourceDefinition)));
    SymbolTable symbolTable = _provider.getSymbolTable("https://Host:100/service|Test--332004310");
    Assert.assertNotNull(symbolTable);
}
Also used : InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) Test(org.testng.annotations.Test)

Example 7 with SymbolTable

use of com.linkedin.data.codec.symbol.SymbolTable in project rest.li by linkedin.

the class RestLiSymbolTableProvider method getSymbolTable.

@Override
public SymbolTable getSymbolTable(String symbolTableName) {
    try {
        SymbolTableMetadata metadata = _symbolTableNameHandler.extractMetadata(symbolTableName);
        String serverNodeUri = metadata.getServerNodeUri();
        String tableName = metadata.getSymbolTableName();
        boolean isRemote = metadata.isRemote();
        // Check if it's the default table name.
        if (tableName.equals(_defaultResponseSymbolTableName)) {
            return _defaultResponseSymbolTable;
        }
        // First check the cache.
        SymbolTable symbolTable = _symbolTableNameToSymbolTableCache.getIfPresent(tableName);
        if (symbolTable != null) {
            return symbolTable;
        }
        // If this is a local table, and we didn't find it in the cache, cry foul.
        if (!isRemote) {
            throw new IllegalStateException("Unable to fetch symbol table with name: " + symbolTableName);
        }
        // Ok, we didn't find it in the cache, let's go query the service the table was served from.
        URI symbolTableUri = new URI(serverNodeUri + "/" + RestLiSymbolTableRequestHandler.SYMBOL_TABLE_URI_PATH + "/" + tableName);
        symbolTable = fetchRemoteSymbolTable(symbolTableUri, Collections.emptyMap(), false);
        if (symbolTable != null) {
            // Cache the retrieved table.
            _symbolTableNameToSymbolTableCache.put(tableName, symbolTable);
            return symbolTable;
        }
    } catch (URISyntaxException ex) {
        LOGGER.error("Failed to construct symbol table URI from symbol table name: " + symbolTableName, ex);
    }
    throw new IllegalStateException("Unable to fetch symbol table with name: " + symbolTableName);
}
Also used : SymbolTableMetadata(com.linkedin.data.codec.symbol.SymbolTableMetadata) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) EmptySymbolTable(com.linkedin.data.codec.symbol.EmptySymbolTable) ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 8 with SymbolTable

use of com.linkedin.data.codec.symbol.SymbolTable in project rest.li by linkedin.

the class TestRestLiSymbolTableProvider method testGetResponseSymbolTableAfterInit.

@Test
public void testGetResponseSymbolTableAfterInit() {
    _provider.onInitialized(Collections.unmodifiableMap(Collections.singletonMap("TestResourceName", _resourceDefinition)));
    SymbolTable symbolTable = _provider.getResponseSymbolTable(URI.create("https://Host:100/service/symbolTable"), Collections.emptyMap());
    Assert.assertNotNull(symbolTable);
    Assert.assertEquals(39, symbolTable.size());
    Assert.assertEquals("https://Host:100/service|Test--332004310", symbolTable.getName());
}
Also used : InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) Test(org.testng.annotations.Test)

Example 9 with SymbolTable

use of com.linkedin.data.codec.symbol.SymbolTable in project rest.li by linkedin.

the class TestRestLiSymbolTableProvider method testGetRemoteSymbolTableFetchSuccess.

@Test
public void testGetRemoteSymbolTableFetchSuccess() throws IOException {
    RestResponseBuilder builder = new RestResponseBuilder();
    builder.setStatus(200);
    SymbolTable symbolTable = new InMemorySymbolTable("https://OtherHost:100/service|Test--332004310", Collections.unmodifiableList(Arrays.asList("Haha", "Hehe")));
    builder.setEntity(SymbolTableSerializer.toByteString(ContentType.PROTOBUF2.getCodec(), symbolTable));
    builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, ContentType.PROTOBUF2.getHeaderKey());
    when(_client.restRequest(eq(new RestRequestBuilder(URI.create("https://OtherHost:100/service/symbolTable/Test--332004310")).setHeaders(Collections.singletonMap(RestConstants.HEADER_FETCH_SYMBOL_TABLE, Boolean.TRUE.toString())).build()))).thenReturn(CompletableFuture.completedFuture(builder.build()));
    SymbolTable remoteSymbolTable = _provider.getSymbolTable("https://OtherHost:100/service|Test--332004310");
    Assert.assertNotNull(remoteSymbolTable);
    Assert.assertEquals("https://Host:100/service|Test--332004310", remoteSymbolTable.getName());
    Assert.assertEquals(2, remoteSymbolTable.size());
    // Subsequent fetch should not trigger network fetch and get the table from the cache.
    when(_client.restRequest(any(RestRequest.class))).thenThrow(new IllegalStateException());
    SymbolTable cachedSymbolTable = _provider.getSymbolTable("https://OtherHost:100/service|Test--332004310");
    Assert.assertSame(remoteSymbolTable, cachedSymbolTable);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Example 10 with SymbolTable

use of com.linkedin.data.codec.symbol.SymbolTable in project rest.li by linkedin.

the class TestRestLiSymbolTableProvider method testGetRemoteRequestSymbolTableFetchSuccess.

@Test
public void testGetRemoteRequestSymbolTableFetchSuccess() throws IOException {
    RestResponseBuilder builder = new RestResponseBuilder();
    builder.setStatus(200);
    SymbolTable symbolTable = new InMemorySymbolTable("https://OtherHost:100/service|Test--332004310", Collections.unmodifiableList(Arrays.asList("Haha", "Hehe")));
    builder.setEntity(SymbolTableSerializer.toByteString(ContentType.PROTOBUF2.getCodec(), symbolTable));
    builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, ContentType.PROTOBUF2.getHeaderKey());
    when(_client.restRequest(eq(new RestRequestBuilder(URI.create("d2://someservice/symbolTable")).setHeaders(Collections.singletonMap(RestConstants.HEADER_FETCH_SYMBOL_TABLE, Boolean.TRUE.toString())).build()))).thenReturn(CompletableFuture.completedFuture(builder.build()));
    SymbolTable remoteSymbolTable = _provider.getRequestSymbolTable(URI.create("d2://someservice/path"));
    Assert.assertNotNull(remoteSymbolTable);
    Assert.assertEquals("https://Host:100/service|Test--332004310", remoteSymbolTable.getName());
    Assert.assertEquals(2, remoteSymbolTable.size());
    // Subsequent fetch should not trigger network fetch and get the table from the cache, regardless of
    // whether the table is fetched by request URI or symbol table name.
    when(_client.restRequest(any(RestRequest.class))).thenThrow(new IllegalStateException());
    SymbolTable cachedSymbolTable = _provider.getRequestSymbolTable(URI.create("d2://someservice/path"));
    Assert.assertSame(remoteSymbolTable, cachedSymbolTable);
    cachedSymbolTable = _provider.getSymbolTable("https://OtherHost:100/service|Test--332004310");
    Assert.assertSame(remoteSymbolTable, cachedSymbolTable);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Aggregations

SymbolTable (com.linkedin.data.codec.symbol.SymbolTable)13 InMemorySymbolTable (com.linkedin.data.codec.symbol.InMemorySymbolTable)12 Test (org.testng.annotations.Test)8 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 URI (java.net.URI)6 ByteString (com.linkedin.data.ByteString)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 RequestContext (com.linkedin.r2.message.RequestContext)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)3 EmptySymbolTable (com.linkedin.data.codec.symbol.EmptySymbolTable)2 SymbolTableProvider (com.linkedin.data.codec.symbol.SymbolTableProvider)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DataMap (com.linkedin.data.DataMap)1 ProtobufCodecOptions (com.linkedin.data.codec.ProtobufCodecOptions)1 DefaultSymbolTableProvider (com.linkedin.data.codec.symbol.DefaultSymbolTableProvider)1 SymbolTableMetadata (com.linkedin.data.codec.symbol.SymbolTableMetadata)1