Exemplo n.º 1
0
@Path("/v2/event")
public class EventResource {
  private final Set<EventWriter> writers;
  private final Set<String> acceptedEventTypes;
  private final ScheduledExecutorService executor;
  private final EventClient eventClient;
  private final MapMaker maker = new MapMaker();
  private ConcurrentMap<String, ProcessStats> stats = maker.makeMap();

  @Inject
  public EventResource(
      Set<EventWriter> writers,
      ServerConfig config,
      ScheduledExecutorService executor,
      EventClient eventClient) {
    Preconditions.checkNotNull(writers, "writer must not be null");
    this.writers = writers;
    this.acceptedEventTypes = ImmutableSet.copyOf(config.getAcceptedEventTypes());
    this.stats = Maps.newConcurrentMap();
    this.executor = executor;
    this.eventClient = eventClient;
  }

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response post(List<Event> events) throws IOException {
    Set<String> badEvents = Sets.newHashSet();
    for (Event event : events) {
      if (acceptedEventType(event.getType())) {
        for (EventWriter writer : writers) {
          writer.write(event);
        }
        increment(event.getType());
      } else {
        badEvents.add(event.getType());
      }
    }
    if (!badEvents.isEmpty()) {
      String errorMessage = "Invalid event type(s): " + Joiner.on(", ").join(badEvents);
      return Response.status(Status.BAD_REQUEST).entity(errorMessage).build();
    }
    return Response.status(Response.Status.ACCEPTED).build();
  }

  private boolean acceptedEventType(String type) {
    return acceptedEventTypes.isEmpty() || acceptedEventTypes.contains(type);
  }

  private void increment(String eventType) {
    ProcessStats eventStats = stats.get(eventType);
    if (eventStats == null) {
      stats.putIfAbsent(eventType, new ProcessStats(executor, eventClient, eventType));
      eventStats = stats.get(eventType);
      eventStats.start();
    }
    eventStats.processed(1);
  }
}
Exemplo n.º 2
0
  /**
   * Creates a new cache instance.
   *
   * @param name a human readable identifier for this cache. Note that this value will be used to
   *     derive a directory name if the disk cache is enabled, so don't get too creative here (camel
   *     case names work great)
   * @param initialCapacity the initial element size of the cache
   * @param expirationInMinutes time in minutes after which elements will be purged from the cache
   *     (NOTE: this only affects the memory cache, the disk cache does currently NOT handle element
   *     TTLs!)
   * @param maxConcurrentThreads how many threads you think may at once access the cache; this need
   *     not be an exact number, but it helps in fragmenting the cache properly
   */
  public AbstractCache(
      String name, int initialCapacity, long expirationInMinutes, int maxConcurrentThreads) {

    this.name = name;

    MapMaker mapMaker = new MapMaker();
    mapMaker.initialCapacity(initialCapacity);
    mapMaker.expiration(expirationInMinutes * 60, TimeUnit.SECONDS);
    mapMaker.concurrencyLevel(maxConcurrentThreads);
    mapMaker.softValues();
    this.cache = mapMaker.makeMap();
  }
  public SocialDataCaches(Duration expiresAfter) {
    final MapMaker cacheTemplate =
        new MapMaker()
            .softValues()
            .expireAfterWrite(expiresAfter.getMillis(), TimeUnit.MILLISECONDS);

    perUserCaches =
        cacheTemplate.makeComputingMap(
            new Function<UserRef, ConcurrentMap<K, V>>() {
              @Override
              public ConcurrentMap<K, V> apply(UserRef from) {
                return cacheTemplate.makeMap();
              }
            });

    anonCache = cacheTemplate.makeMap();
  }
Exemplo n.º 4
0
 public SessionCache() {
   final MapMaker mapMaker = new MapMaker();
   sessions = mapMaker.makeMap();
   sessionCallbackMap = mapMaker.makeMap();
 }