Search in sources :

Example 11 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class LookupCoordinatorResource method createOrUpdateLookup.

@POST
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Path("/{tier}/{lookup}")
public Response createOrUpdateLookup(@PathParam("tier") String tier, @PathParam("lookup") String lookup, @HeaderParam(AuditManager.X_DRUID_AUTHOR) @DefaultValue("") final String author, @HeaderParam(AuditManager.X_DRUID_COMMENT) @DefaultValue("") final String comment, InputStream in, @Context HttpServletRequest req) {
    try {
        if (Strings.isNullOrEmpty(tier)) {
            return Response.status(Response.Status.BAD_REQUEST).entity(ServletResourceUtils.sanitizeException(new NullPointerException("`tier` required"))).build();
        }
        if (Strings.isNullOrEmpty(lookup)) {
            return Response.status(Response.Status.BAD_REQUEST).entity(ServletResourceUtils.sanitizeException(new IAE("`lookup` required"))).build();
        }
        final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(req.getContentType());
        final ObjectMapper mapper = isSmile ? smileMapper : jsonMapper;
        final Map<String, Object> lookupSpec;
        try {
            lookupSpec = mapper.readValue(in, new TypeReference<Map<String, Object>>() {
            });
        } catch (IOException e) {
            return Response.status(Response.Status.BAD_REQUEST).entity(ServletResourceUtils.sanitizeException(e)).build();
        }
        if (lookupCoordinatorManager.updateLookup(tier, lookup, lookupSpec, new AuditInfo(author, comment, req.getRemoteAddr()))) {
            return Response.status(Response.Status.ACCEPTED).build();
        } else {
            throw new RuntimeException("Unknown error updating configuration");
        }
    } catch (Exception e) {
        LOG.error(e, "Error updating tier [%s] lookup [%s]", tier, lookup);
        return Response.serverError().entity(ServletResourceUtils.sanitizeException(e)).build();
    }
}
Also used : AuditInfo(io.druid.audit.AuditInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) IAE(io.druid.java.util.common.IAE) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 12 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class LookupCoordinatorManager method start.

@LifecycleStart
public void start() {
    synchronized (startStopSync) {
        if (started) {
            return;
        }
        if (executorService.isShutdown()) {
            throw new ISE("Cannot restart after stop!");
        }
        lookupMapConfigRef = configManager.watch(LOOKUP_CONFIG_KEY, new TypeReference<Map<String, Map<String, Map<String, Object>>>>() {
        }, null);
        final ListenableScheduledFuture backgroundManagerFuture = this.backgroundManagerFuture = executorService.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                final Map<String, Map<String, Map<String, Object>>> allLookupTiers = lookupMapConfigRef.get();
                // Sanity check for if we are shutting down
                if (Thread.currentThread().isInterrupted()) {
                    LOG.info("Not updating lookups because process was interrupted");
                    return;
                }
                if (!started) {
                    LOG.info("Not started. Returning");
                    return;
                }
                if (allLookupTiers == null) {
                    LOG.info("Not updating lookups because no data exists");
                    return;
                }
                for (final String tier : allLookupTiers.keySet()) {
                    try {
                        final Map<String, Map<String, Object>> allLookups = allLookupTiers.get(tier);
                        final Map<String, Map<String, Object>> oldLookups = prior_update.get(tier);
                        final Collection<String> drops;
                        if (oldLookups == null) {
                            drops = ImmutableList.of();
                        } else {
                            drops = Sets.difference(oldLookups.keySet(), allLookups.keySet());
                        }
                        if (allLookupTiers == prior_update) {
                            LOG.debug("No updates");
                            updateAllNewOnTier(tier, allLookups);
                        } else {
                            updateAllOnTier(tier, allLookups);
                            deleteAllOnTier(tier, drops);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        throw Throwables.propagate(e);
                    } catch (Exception e) {
                        LOG.error(e, "Error updating lookups for tier [%s]. Will try again soon", tier);
                    }
                }
                prior_update = allLookupTiers;
            }
        }, 0, lookupCoordinatorManagerConfig.getPeriod(), TimeUnit.MILLISECONDS);
        Futures.addCallback(backgroundManagerFuture, new FutureCallback<Object>() {

            @Override
            public void onSuccess(@Nullable Object result) {
                backgroundManagerExitedLatch.countDown();
                LOG.debug("Exited background lookup manager");
            }

            @Override
            public void onFailure(Throwable t) {
                backgroundManagerExitedLatch.countDown();
                if (backgroundManagerFuture.isCancelled()) {
                    LOG.info("Background lookup manager exited");
                    LOG.trace(t, "Background lookup manager exited with throwable");
                } else {
                    LOG.makeAlert(t, "Background lookup manager exited with error!").emit();
                }
            }
        });
        started = true;
        LOG.debug("Started");
    }
}
Also used : TimeoutException(java.util.concurrent.TimeoutException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ListenableScheduledFuture(com.google.common.util.concurrent.ListenableScheduledFuture) ISE(io.druid.java.util.common.ISE) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LifecycleStart(io.druid.java.util.common.lifecycle.LifecycleStart)

Example 13 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class LookupCoordinatorResource method updateAllLookups.

@POST
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
public Response updateAllLookups(InputStream in, @HeaderParam(AuditManager.X_DRUID_AUTHOR) @DefaultValue("") final String author, @HeaderParam(AuditManager.X_DRUID_COMMENT) @DefaultValue("") final String comment, @Context HttpServletRequest req) {
    try {
        final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(req.getContentType());
        final ObjectMapper mapper = isSmile ? smileMapper : jsonMapper;
        final Map<String, Map<String, Map<String, Object>>> map;
        try {
            map = mapper.readValue(in, new TypeReference<Map<String, Map<String, Map<String, Object>>>>() {
            });
        } catch (IOException e) {
            return Response.status(Response.Status.BAD_REQUEST).entity(ServletResourceUtils.sanitizeException(e)).build();
        }
        if (lookupCoordinatorManager.updateLookups(map, new AuditInfo(author, comment, req.getRemoteAddr()))) {
            return Response.status(Response.Status.ACCEPTED).entity(map).build();
        } else {
            throw new RuntimeException("Unknown error updating configuration");
        }
    } catch (Exception e) {
        LOG.error(e, "Error creating new lookups");
        return Response.serverError().entity(ServletResourceUtils.sanitizeException(e)).build();
    }
}
Also used : AuditInfo(io.druid.audit.AuditInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 14 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class SQLMetadataRuleManagerTest method testAuditEntryCreated.

@Test
public void testAuditEntryCreated() throws Exception {
    List<Rule> rules = Arrays.<Rule>asList(new IntervalLoadRule(new Interval("2015-01-01/2015-02-01"), ImmutableMap.<String, Integer>of(DruidServer.DEFAULT_TIER, DruidServer.DEFAULT_NUM_REPLICANTS)));
    AuditInfo auditInfo = new AuditInfo("test_author", "test_comment", "127.0.0.1");
    ruleManager.overrideRule("test_dataSource", rules, auditInfo);
    // fetch rules from metadata storage
    ruleManager.poll();
    Assert.assertEquals(rules, ruleManager.getRules("test_dataSource"));
    // verify audit entry is created
    List<AuditEntry> auditEntries = auditManager.fetchAuditHistory("test_dataSource", "rules", null);
    Assert.assertEquals(1, auditEntries.size());
    AuditEntry entry = auditEntries.get(0);
    Assert.assertEquals(rules, mapper.readValue(entry.getPayload(), new TypeReference<List<Rule>>() {
    }));
    Assert.assertEquals(auditInfo, entry.getAuditInfo());
    Assert.assertEquals("test_dataSource", entry.getKey());
}
Also used : AuditInfo(io.druid.audit.AuditInfo) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) AuditEntry(io.druid.audit.AuditEntry) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) Rule(io.druid.server.coordinator.rules.Rule) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 15 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project druid by druid-io.

the class SQLMetadataRuleManagerTest method testFetchAuditEntriesForAllDataSources.

@Test
public void testFetchAuditEntriesForAllDataSources() throws Exception {
    List<Rule> rules = Arrays.<Rule>asList(new IntervalLoadRule(new Interval("2015-01-01/2015-02-01"), ImmutableMap.<String, Integer>of(DruidServer.DEFAULT_TIER, DruidServer.DEFAULT_NUM_REPLICANTS)));
    AuditInfo auditInfo = new AuditInfo("test_author", "test_comment", "127.0.0.1");
    ruleManager.overrideRule("test_dataSource", rules, auditInfo);
    ruleManager.overrideRule("test_dataSource2", rules, auditInfo);
    // fetch rules from metadata storage
    ruleManager.poll();
    Assert.assertEquals(rules, ruleManager.getRules("test_dataSource"));
    Assert.assertEquals(rules, ruleManager.getRules("test_dataSource2"));
    // test fetch audit entries
    List<AuditEntry> auditEntries = auditManager.fetchAuditHistory("rules", null);
    Assert.assertEquals(2, auditEntries.size());
    for (AuditEntry entry : auditEntries) {
        Assert.assertEquals(rules, mapper.readValue(entry.getPayload(), new TypeReference<List<Rule>>() {
        }));
        Assert.assertEquals(auditInfo, entry.getAuditInfo());
    }
}
Also used : AuditInfo(io.druid.audit.AuditInfo) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) AuditEntry(io.druid.audit.AuditEntry) IntervalLoadRule(io.druid.server.coordinator.rules.IntervalLoadRule) Rule(io.druid.server.coordinator.rules.Rule) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Interval(org.joda.time.Interval) Test(org.junit.Test)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)67 IOException (java.io.IOException)27 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)25 Test (org.junit.Test)23 Map (java.util.Map)13 BaseMandrillAnonymousListResponse (com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillAnonymousListResponse)7 StringWriter (java.io.StringWriter)7 ArrayList (java.util.ArrayList)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 List (java.util.List)5 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)4 AuditInfo (io.druid.audit.AuditInfo)4 Interval (org.joda.time.Interval)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)3