Skip to content

Commit

Permalink
Make global lookup cache introspector class public (apache#3199)
Browse files Browse the repository at this point in the history
* Make global lookup cache introspector class public
* Fixes apache#3187

* Make KafkaLookupExtractorIntrospectionHandler a public static class
  • Loading branch information
drcrallen authored and nishantmonu51 committed Jul 1, 2016
1 parent e1313e4 commit bfa5c05
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@

import javax.annotation.Nullable;
import javax.validation.constraints.Min;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -344,7 +342,7 @@ && isInjective() == that.isInjective()
@Override
public LookupIntrospectHandler getIntrospectHandler()
{
return new KafkaLookupExtractorIntrospectionHandler();
return new KafkaLookupExtractorIntrospectionHandler(this);
}

@Override
Expand Down Expand Up @@ -406,19 +404,4 @@ ListenableFuture<?> getFuture()
{
return future;
}


class KafkaLookupExtractorIntrospectionHandler implements LookupIntrospectHandler
{
@GET
public Response getActive()
{
final ListenableFuture<?> future = getFuture();
if (future != null && !future.isDone()) {
return Response.ok().build();
} else {
return Response.status(Response.Status.GONE).build();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.query.lookup;

import com.google.common.util.concurrent.ListenableFuture;

import javax.ws.rs.GET;
import javax.ws.rs.core.Response;

public class KafkaLookupExtractorIntrospectionHandler implements LookupIntrospectHandler
{
private KafkaLookupExtractorFactory kafkaLookupExtractorFactory;

public KafkaLookupExtractorIntrospectionHandler(KafkaLookupExtractorFactory kafkaLookupExtractorFactory) {this.kafkaLookupExtractorFactory = kafkaLookupExtractorFactory;}

@GET
public Response getActive()
{
final ListenableFuture<?> future = kafkaLookupExtractorFactory.getFuture();
if (future != null && !future.isDone()) {
return Response.ok().build();
} else {
return Response.status(Response.Status.GONE).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.metamx.common.ISE;
import com.metamx.common.StringUtils;
import com.metamx.common.logger.Logger;
import io.druid.common.utils.ServletResourceUtils;
import io.druid.query.extraction.MapLookupExtractor;
import io.druid.query.lookup.namespace.ExtractionNamespace;
import io.druid.server.lookup.namespace.cache.NamespaceExtractionCacheManager;

import javax.annotation.Nullable;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -86,65 +79,7 @@ public NamespaceLookupExtractorFactory(
this.injective = injective;
this.manager = manager;
this.extractorID = buildID();
this.lookupIntrospectHandler = new LookupIntrospectHandler()
{
@GET
@Path("/keys")
@Produces(MediaType.APPLICATION_JSON)
public Response getKeys()
{
try {
return Response.ok(getLatest().keySet()).build();
}
catch (ISE e) {
return Response.status(Response.Status.NOT_FOUND).entity(ServletResourceUtils.sanitizeException(e)).build();
}
}

@GET
@Path("/values")
@Produces(MediaType.APPLICATION_JSON)
public Response getValues()
{
try {
return Response.ok(getLatest().values()).build();
}
catch (ISE e) {
return Response.status(Response.Status.NOT_FOUND).entity(ServletResourceUtils.sanitizeException(e)).build();
}
}

@GET
@Path("/version")
@Produces(MediaType.APPLICATION_JSON)
public Response getVersion()
{
final String version = manager.getVersion(extractorID);
if (null == version) {
// Handle race between delete and this method being called
return Response.status(Response.Status.NOT_FOUND).build();
} else {
return Response.ok(ImmutableMap.of("version", version)).build();
}
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMap()
{
try {
return Response.ok(getLatest()).build();
}
catch (ISE e) {
return Response.status(Response.Status.NOT_FOUND).entity(ServletResourceUtils.sanitizeException(e)).build();
}
}

private Map<String, String> getLatest()
{
return ((MapLookupExtractor) get()).getMap();
}
};
this.lookupIntrospectHandler = new NamespaceLookupIntrospectHandler(this, manager, extractorID);
}

@VisibleForTesting
Expand All @@ -166,13 +101,13 @@ public boolean start()
LOG.warn("Already started! [%s]", extractorID);
return true;
}
if(firstCacheTimeout > 0) {
if (firstCacheTimeout > 0) {
if (!manager.scheduleAndWait(extractorID, extractionNamespace, firstCacheTimeout)) {
LOG.error("Failed to schedule and wait for lookup [%s]", extractorID);
return false;
}
} else {
if(!manager.scheduleOrUpdate(extractorID, extractionNamespace)) {
if (!manager.scheduleOrUpdate(extractorID, extractionNamespace)) {
LOG.error("Failed to schedule lookup [%s]", extractorID);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.query.lookup;

import com.google.common.collect.ImmutableMap;
import com.metamx.common.ISE;
import io.druid.common.utils.ServletResourceUtils;
import io.druid.query.extraction.MapLookupExtractor;
import io.druid.server.lookup.namespace.cache.NamespaceExtractionCacheManager;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

public class NamespaceLookupIntrospectHandler implements LookupIntrospectHandler
{
private final NamespaceLookupExtractorFactory factory;
private final String extractorID;
private final NamespaceExtractionCacheManager manager;
public NamespaceLookupIntrospectHandler(
NamespaceLookupExtractorFactory factory,
NamespaceExtractionCacheManager manager,
String extractorID
) {
this.factory = factory;
this.extractorID = extractorID;
this.manager = manager;
}
@GET
@Path("/keys")
@Produces(MediaType.APPLICATION_JSON)
public Response getKeys()
{
try {
return Response.ok(getLatest().keySet()).build();
}
catch (ISE e) {
return Response.status(Response.Status.NOT_FOUND).entity(ServletResourceUtils.sanitizeException(e)).build();
}
}

@GET
@Path("/values")
@Produces(MediaType.APPLICATION_JSON)
public Response getValues()
{
try {
return Response.ok(getLatest().values()).build();
}
catch (ISE e) {
return Response.status(Response.Status.NOT_FOUND).entity(ServletResourceUtils.sanitizeException(e)).build();
}
}

@GET
@Path("/version")
@Produces(MediaType.APPLICATION_JSON)
public Response getVersion()
{
final String version = manager.getVersion(extractorID);
if (null == version) {
// Handle race between delete and this method being called
return Response.status(Response.Status.NOT_FOUND).build();
} else {
return Response.ok(ImmutableMap.of("version", version)).build();
}
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMap()
{
try {
return Response.ok(getLatest()).build();
}
catch (ISE e) {
return Response.status(Response.Status.NOT_FOUND).entity(ServletResourceUtils.sanitizeException(e)).build();
}
}

private Map<String, String> getLatest()
{
return ((MapLookupExtractor) factory.get()).getMap();
}
}

0 comments on commit bfa5c05

Please sign in to comment.