Example #1
0
static mtev_hook_return_t
zipkin_fq_publish(void *closure, int64_t traceid, int64_t spanid,
                  unsigned char *buf, size_t len) {
  if(N_L_S_ON(debugls)) {
    int blen;
    char *b64buf;
    b64buf = malloc(len*2);
    blen = mtev_b64_encode(buf,len,b64buf,len*3);
    b64buf[blen] = '\0';
    mtevL(debugls,"%s\n", b64buf);
    free(b64buf);
  }
  if(zc_client != NULL) {
    char buff[128];
    fq_msg *msg;
    snprintf(buff, sizeof(buff), "%s%llx", zc_routing_prefix, (long long int)traceid);
    msg = fq_msg_alloc(buf, len);
    fq_msg_id(msg,NULL);
    fq_msg_route(msg, buff, strlen(buff));
    fq_msg_exchange(msg, zc_exchange, strlen(zc_exchange));
    fq_client_publish(zc_client, msg);
    fq_msg_free(msg);
  }
  return MTEV_HOOK_CONTINUE;
}
Example #2
0
int main(int argc, char **argv) {
  hrtime_t s0, s, f, f0;
  uint64_t cnt = 0, icnt = 0;
  int psize = 0, i = 0;
  fq_client c;
  fq_msg *m;
  char *fq_debug = getenv("FQ_DEBUG");
  if(fq_debug) fq_debug_set_bits(atoi(fq_debug));
  signal(SIGPIPE, SIG_IGN);
  fq_client_init(&c, 0, logger);
  if(argc < 5) {
    fprintf(stderr, "%s <host> <port> <user> <pass> [size [count]]\n",
            argv[0]);
    exit(-1);
  }
  fq_client_creds(c, argv[1], atoi(argv[2]), argv[3], argv[4]);
  fq_client_heartbeat(c, 1000);
  fq_client_set_backlog(c, 10000, 100);
  fq_client_connect(c);

  if(argc > 5) {
     psize = atoi(argv[5]);
  }
  printf("payload size -> %d\n", psize);
  if(argc > 6) {
    send_count = atoi(argv[6]);
  }
  printf("message count -> %d\n", send_count);

  s0 = s = fq_gethrtime();
  while(i < send_count || fq_client_data_backlog(c) > 0) {
    if(i < send_count) {
      m = fq_msg_alloc_BLANK(psize);
      memset(m->payload, 0, psize);
      fq_msg_exchange(m, "maryland", 8);
      fq_msg_route(m, "test.prefix.boo", 15);
      fq_msg_id(m, NULL);
      fq_client_publish(c, m);
      cnt++;
      i++;
      fq_msg_free(m);
    }
    else usleep(100);


    f = fq_gethrtime();
    if(f-s > 1000000000) {
      print_rate(c, s, f, cnt, icnt);
      icnt = 0;
      cnt = 0;
      s = f;
    }
  }
  f0 = fq_gethrtime();
  print_rate(c, s0, f0, i, 0);
  (void) argc;
  return 0;
}
Example #3
0
static int
noit_fq_submit(iep_thread_driver_t *dr,
               const char *payload, size_t payloadlen) {
  int i;
  struct fq_driver *driver = (struct fq_driver *)dr;
  const char *routingkey = driver->routingkey;
  fq_msg *msg;

  if(*payload == 'M' ||
     *payload == 'S' ||
     *payload == 'C' ||
     (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) {
    char uuid_str[32 * 2 + 1];
    int account_id, check_id;
    if(extract_uuid_from_jlog(payload, payloadlen,
                              &account_id, &check_id, uuid_str)) {
      if(*routingkey) {
        char *replace;
        int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32;
        replace = alloca(newlen);
        snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey,
                 account_id%16, (account_id/16)%16, account_id,
                 check_id, uuid_str);
        routingkey = replace;
      }
    }
  }

  /* Setup our message */
  msg = fq_msg_alloc(payload, payloadlen);
  if(msg == NULL) {
    driver->allocation_failures++;
    return -1;
  }
  driver->msg_cnt++;
  fq_msg_exchange(msg, driver->exchange, strlen(driver->exchange));
  noitL(noit_debug, "route[%s] -> %s\n", driver->exchange, routingkey);
  fq_msg_route(msg, routingkey, strlen(routingkey));
  fq_msg_id(msg, NULL);

  for(i=0; i<driver->nhosts; i++) {
    if(fq_client_publish(driver->client[i], msg) == 1)
      BUMPSTAT(i, publications);
    else
      BUMPSTAT(i, client_tx_drop);
  }
  fq_msg_deref(msg);
  return 0;
}
Example #4
0
File: fqc.c Project: denji/fq
int main(int argc, char **argv) {
  hrtime_t s0, s, f, f0;
  uint64_t cnt = 0, icnt = 0;
  int psize = 0, i = 0, rcvd = 0;
  fq_client c;
  fq_bind_req breq;
  fq_msg *m;
  signal(SIGPIPE, SIG_IGN);
  fq_client_init(&c, 0, logger);
  if(argc < 5) {
    fprintf(stderr, "%s <host> <port> <user> <pass> [size [count]]\n",
            argv[0]);
    exit(-1);
  }
  fq_client_creds(c, argv[1], atoi(argv[2]), argv[3], argv[4]);
  fq_client_heartbeat(c, 1000);
  fq_client_set_backlog(c, 10000, 100);
  fq_client_connect(c);

  memset(&breq, 0, sizeof(breq));
  memcpy(breq.exchange.name, "maryland", 8);
  breq.exchange.len = 8;
  breq.peermode = 0;
  breq.program = (char *)"prefix:\"test.prefix.\"";

  fq_client_bind(c, &breq);
  while(breq.out__route_id == 0) usleep(100);
  printf("route set -> %u\n", breq.out__route_id);
  if(breq.out__route_id == FQ_BIND_ILLEGAL) {
    fprintf(stderr, "Failure to bind...\n");
    exit(-1);
  }

  if(argc > 5) {
     psize = atoi(argv[5]);
  }
  printf("payload size -> %d\n", psize);
  if(argc > 6) {
    send_count = atoi(argv[6]);
  }
  printf("message count -> %d\n", send_count);

  s0 = s = fq_gethrtime();
  while(i < send_count || fq_client_data_backlog(c) > 0) {
    if(i < send_count) {
      m = fq_msg_alloc_BLANK(psize);
      memset(m->payload, 0, psize);
      fq_msg_exchange(m, "maryland", 8);
      fq_msg_route(m, "test.prefix.foo", 15);
      fq_msg_id(m, NULL);
      fq_client_publish(c, m);
      cnt++;
      i++;
      fq_msg_free(m);
    }
    else usleep(100);


    f = fq_gethrtime();
    while(m = fq_client_receive(c)) {
      icnt++;
      rcvd++;
      fq_msg_deref(m);
    }
    if(f-s > 1000000000) {
      print_rate(c, s, f, cnt, icnt);
      icnt = 0;
      cnt = 0;
      s = f;
    }
  }
  f0 = fq_gethrtime();
  print_rate(c, s0, f0, i, 0);
  do {
    icnt=0;
    while(m = fq_client_receive(c)) {
      icnt++;
      rcvd++;
      fq_msg_deref(m);
    }
  } while(rcvd < send_count);
  f0 = fq_gethrtime();
  print_rate(c, s0, f0, 0, rcvd);
  printf("Total received during test: %d\n", rcvd);

  (void) argc;
  return 0;
}
Example #5
0
int main(int argc, char **argv) {
  char *host = NULL, *exchange = NULL, *route = NULL;
  char *address = strdup("127.0.0.1:8765");
  char *user = "******";
  char *pass = "******";
  int port = 0;
  int c = 0;
  if (argc == 1) {
    usage(argv[0]);
    exit(-1);
  }
  while((c = getopt(argc, argv, "ha:x:r:u:p:")) != EOF) {
    switch(c) {
    case 'h':
      usage(argv[0]);
      exit(0);
      break;
    case 'a':
      address = strdup(optarg);
      break;
    case 'x':
      exchange = strdup(optarg);
      break;
    case 'r':
      route = strdup(optarg);
      break;
    case 'u':
      user = strdup(optarg);
      break;
    case 'p':
      pass = strdup(optarg);
      break;
    default:
      usage(argv[0]);
      exit(-1);
    }
  }
  // Separate host and port in address string
  // We have strdup'ed address so we can mutate it
  host = address;
  for (char *p = address; *p != 0; p++) {
    if (*p == ':') {
      *p = 0;
      port = strtol(p+1, NULL, 10);
      break;
    }
  }
  if (!port) {
    printf("Illegal port specification\n");
    exit(-1);
  }
  if (!exchange) {
    printf("Exchange argument required");
    exit(-1);
  }
  if (!route) {
    printf("Route argument required");
    exit(-1);
  }

  fq_client cli;
  fq_msg *m = NULL;
  size_t exchange_len = strlen(exchange);
  size_t route_len = strlen(route);
  signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE
  fq_client_init(&cli, 0, logger);
  fq_client_creds(cli, host, port, user, pass);
  fq_client_heartbeat(cli, 1000);
  fq_client_set_backlog(cli, 10000, 100);
  fq_client_connect(cli);
  while(true) {
    char *line = NULL;
    size_t line_cap = 0;
    int line_len;
    line_len = getline(&line, &line_cap, stdin);
    if(line_len < 0) {
      // wait for queues to drain
      while(fq_client_data_backlog(cli) > 0) {
        usleep(100);
      }
      exit(0);
    }
    m = fq_msg_alloc(line, line_len);
    fq_msg_exchange(m, exchange, exchange_len);
    fq_msg_route(m, route, route_len);
    fq_msg_id(m, NULL);
    fq_client_publish(cli, m);
    fq_msg_free(m);
  }
  return 0;
}
Example #6
0
static int
noit_fq_submit(iep_thread_driver_t *dr,
               const char *payload, size_t payloadlen) {
  int i;
  struct fq_driver *driver = (struct fq_driver *)dr;
  const char *routingkey = driver->routingkey;
  mtev_hash_table *filtered_metrics;
  char uuid_formatted_str[UUID_STR_LEN+1];
  bool is_bundle = false, is_metric = false, send = false;
  fq_msg *msg;
  char *metric = NULL;

  if(*payload == 'M' ||
     *payload == 'S' ||
     *payload == 'C' ||
     (*payload == 'H' && payload[1] == '1') ||
     (*payload == 'F' && payload[1] == '1') ||
     (*payload == 'B' && (payload[1] == '1' || payload[1] == '2'))) {
    char uuid_str[32 * 2 + 1];
    int account_id, check_id;

    if (*payload == 'B') is_bundle = true;
    else if ((*payload == 'H') || (*payload == 'M')) {
      is_metric = true;
    }

    if(extract_uuid_from_jlog(payload, payloadlen,
                              &account_id, &check_id, uuid_str,
                              uuid_formatted_str, &metric)) {
      if(*routingkey) {
        char *replace;
        int newlen = strlen(driver->routingkey) + 1 + sizeof(uuid_str) + 2 * 32;
        replace = alloca(newlen);
        snprintf(replace, newlen, "%s.%x.%x.%d.%d%s", driver->routingkey,
                 account_id%16, (account_id/16)%16, account_id,
                 check_id, uuid_str);
        routingkey = replace;
      }
    }
  }

  /* Let through any messages that aren't metrics or bundles */
  if (!is_bundle && !is_metric) {
    send = true;
  }

  /* Setup our message */
  msg = fq_msg_alloc(payload, payloadlen);
  if(msg == NULL) {
    driver->allocation_failures++;
    if (metric) free(metric);
    return -1;
  }
  driver->msg_cnt++;
  fq_msg_exchange(msg, driver->exchange, strlen(driver->exchange));
  mtevL(mtev_debug, "route[%s] -> %s\n", driver->exchange, routingkey);
  fq_msg_route(msg, routingkey, strlen(routingkey));
  fq_msg_id(msg, NULL);

  if (global_fq_ctx.round_robin) {
    int checked = 0, good = 0;
    time_t cur_time;
    while (1) {
      if (!global_fq_ctx.down_host[global_fq_ctx.round_robin_target]) {
        good = 1;
        break;
      }
      cur_time = time(NULL);
      if (cur_time - global_fq_ctx.last_error[global_fq_ctx.round_robin_target] >= 10) {
        global_fq_ctx.down_host[global_fq_ctx.round_robin_target] = false;
        good = 1;
        break;
      } 
      global_fq_ctx.round_robin_target = (global_fq_ctx.round_robin_target+1) % driver->nhosts;
      checked++;
      if (checked == driver->nhosts) {
        /* This means everybody is down.... just try to send to whatever fq
           we're pointing at */
        break;
      }
    }
    if (good) {
      if(fq_client_publish(driver->client[global_fq_ctx.round_robin_target], msg) == 1) {
        BUMPSTAT(global_fq_ctx.round_robin_target, publications);
      }
      else {
        BUMPSTAT(global_fq_ctx.round_robin_target, client_tx_drop);
      }
    }
    /* Go ahead and try to publish to the hosts that are down, just in
       case they've come back up. This should help minimize lost messages */
    for (i=0; i<driver->nhosts; i++) {
      if (global_fq_ctx.down_host[i]) {
        if(fq_client_publish(driver->client[i], msg) == 1) {
          BUMPSTAT(i, publications);
        }
        else {
          BUMPSTAT(i, client_tx_drop);
        }
      }
    }
    global_fq_ctx.round_robin_target = (global_fq_ctx.round_robin_target+1) % driver->nhosts;
  }
  else {
    for(i=0; i<driver->nhosts; i++) {
      if(fq_client_publish(driver->client[i], msg) == 1) {
        BUMPSTAT(i, publications);
      }
      else {
        BUMPSTAT(i, client_tx_drop);
      }
    }
  }

  fq_msg_deref(msg);
  if (!send) {
    if(mtev_hash_retrieve(&filtered_checks_hash, uuid_formatted_str, strlen(uuid_formatted_str), (void**)&filtered_metrics)) {
      if (is_bundle || (is_metric && mtev_hash_size(filtered_metrics) == 0)) {
        send = true;
      }
      else if (is_metric) {
        void *tmp;
        if (mtev_hash_retrieve(filtered_metrics, metric, strlen(metric), &tmp)) {
          send = true;
        }
      }
    }
  }

  if (global_fq_ctx.filtered_exchange[0] && send) {
    fq_msg *msg2;
    msg2 = fq_msg_alloc(payload, payloadlen);
    fq_msg_exchange(msg2, driver->filtered_exchange, strlen(driver->filtered_exchange));
    mtevL(mtev_debug, "route[%s] -> %s\n", driver->filtered_exchange, routingkey);
    fq_msg_route(msg2, routingkey, strlen(routingkey));
    fq_msg_id(msg2, NULL);
    for(i=0; i<driver->nhosts; i++) {
      if(fq_client_publish(driver->client[i], msg2) == 1) {
        BUMPSTAT(i, publications);
      }
      else {
        BUMPSTAT(i, client_tx_drop);
      }
    }
    fq_msg_deref(msg2);
  }
  if (metric) free(metric);
  return 0;
}