Package com.dianping.cat.message

Examples of com.dianping.cat.message.Transaction


  @Override
  public ModelResponse<T> invoke(final ModelRequest request) {
    int size = m_allServices.size();
    final List<ModelResponse<T>> responses = Collections.synchronizedList(new ArrayList<ModelResponse<T>>(size));
    final Semaphore semaphore = new Semaphore(0);
    final Transaction t = Cat.getProducer().newTransaction("ModelService", getClass().getSimpleName());
    int count = 0;

    t.setStatus(Message.SUCCESS);
    t.addData("request", request);
    t.addData("thread", Thread.currentThread());

    for (final ModelService<T> service : m_allServices) {
      if (!service.isEligable(request)) {
        continue;
      }

      // save current transaction so that child thread can access it
      if (service instanceof ModelServiceWithCalSupport) {
        ((ModelServiceWithCalSupport) service).setParentTransaction(t);
      }

      s_threadPool.submit(new Runnable() {
        @Override
        public void run() {
          Cat.setup("model-service");

          try {
            ModelResponse<T> response = service.invoke(request);

            if (response.getException() != null) {
              logError(response.getException());
            }

            responses.add(response);
          } catch (Exception e) {
            logError(e);
            t.setStatus(e);
          } finally {
            semaphore.release();
            Cat.reset();
          }
        }
      });

      count++;
    }

    try {
      semaphore.tryAcquire(count, 10000, TimeUnit.MILLISECONDS); // 10
                                                                 // seconds
                                                                 // timeout
    } catch (InterruptedException e) {
      // ignore it
      t.setStatus(e);
    } finally {
      t.complete();
    }

    ModelResponse<T> aggregated = new ModelResponse<T>();
    T report = merge(request, responses);
View Full Code Here


    long time = System.currentTimeMillis();
    int i = 10;

    while (i > 0) {
      i++;
      Transaction total = Cat.newTransaction("cat", "Test");
      Transaction t = Cat.getProducer().newTransaction("Cache.kvdb", "Method" + i % 10);
      t.setStatus(Message.SUCCESS);
      Cat.getProducer().newEvent("Cache.kvdb", "Method" + i % 10 + ":missed");
      t.addData("key and value");

      Transaction t2 = Cat.getProducer().newTransaction("Cache.web", "Method" + i % 10);
      Cat.getProducer().newEvent("Cache.web", "Method" + i % 10 + ":missed");
      t2.addData("key and value");
      t2.setStatus(Message.SUCCESS);
      t2.complete();

      Transaction t3 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t3.addData("key and value");
      t3.setStatus(Message.SUCCESS);
      t3.complete();

      Transaction t4 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t4.addData("key and value");
      t4.setStatus(Message.SUCCESS);
      t4.complete();

      Transaction t5 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t5.addData("key and value");
      t5.setStatus(Message.SUCCESS);
      t5.complete();

      Transaction t6 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t6.addData("key and value");
      t6.setStatus(Message.SUCCESS);
      t6.complete();

      Transaction t7 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t7.addData("key and value");
      t7.setStatus(Message.SUCCESS);
      t7.complete();

      Transaction t8 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t8.addData("key and value");
      t8.setStatus(Message.SUCCESS);
      t8.complete();

      Transaction t9 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t9.addData("key and value");
      t9.setStatus(Message.SUCCESS);
      t9.complete();
      t.complete();

      total.setStatus(Transaction.SUCCESS);
      t.complete();
View Full Code Here

  @Test
  public void sendSqlTransaction() throws Exception {
    for (int k = 0; k < 5; k++) {
      for (int i = 0; i < 100; i++) {
        Transaction t = Cat.getProducer().newTransaction("SQL", "User.select" + i % 10);
        Cat.getProducer().newEvent("SQL.Method", "Select").setStatus(Message.SUCCESS);
        Cat.getProducer().newEvent("SQL.Database", "jdbc:mysql://192.168.7.43:3306/database" + k)
              .setStatus(Message.SUCCESS);
        t.addData("select * from hostinfo");
        t.setStatus(Message.SUCCESS);
        t.complete();

        Transaction t2 = Cat.getProducer().newTransaction("SQL", "User.insert" + i % 10);
        Cat.getProducer().newEvent("SQL.Method", "Update").setStatus(Message.SUCCESS);
        Cat.getProducer().newEvent("SQL.Database", "jdbc:mysql://192.168.7.43:3306/database" + k)
              .setStatus(Message.SUCCESS);
        t2.addData("update * from hostinfo");
        t2.complete();

        Transaction t3 = Cat.getProducer().newTransaction("SQL", "User.delete" + i % 10);
        Cat.getProducer().newEvent("SQL.Method", "Delete").setStatus(Message.SUCCESS);
        Cat.getProducer().newEvent("SQL.Database", "jdbc:mysql://192.168.7.43:3306/database" + k)
              .setStatus(Message.SUCCESS);
        t3.addData("delete * from hostinfo");
        t3.setStatus(Message.SUCCESS);
        t3.complete();
      }
    }
    Thread.sleep(1000);
  }
View Full Code Here

  @Test
  public void sendOtherDomainSqlTransaction() throws Exception {
    for (int k = 0; k < 5; k++) {
      for (int i = 0; i < 100; i++) {
        Transaction t = Cat.getProducer().newTransaction("SQL", "User.select" + i % 10);
        Cat.getProducer().newEvent("SQL.Method", "Select").setStatus(Message.SUCCESS);
        Cat.getProducer().newEvent("SQL.Database", "jdbc:mysql://192.168.7.43:3306/database" + k)
              .setStatus(Message.SUCCESS);
        t.addData("select * from hostinfo");
        t.setStatus(Message.SUCCESS);
        Cat.getManager().getThreadLocalMessageTree().setDomain("CatDemo");
        t.complete();

        Transaction t2 = Cat.getProducer().newTransaction("SQL", "User.insert" + i % 10);
        Cat.getProducer().newEvent("SQL.Method", "Update").setStatus(Message.SUCCESS);
        Cat.getProducer().newEvent("SQL.Database", "jdbc:mysql://192.168.7.43:3306/database" + k)
              .setStatus(Message.SUCCESS);
        t2.addData("update * from hostinfo");
        Cat.getManager().getThreadLocalMessageTree().setDomain("CatDemo");
        t2.complete();

        Transaction t3 = Cat.getProducer().newTransaction("SQL", "User.delete" + i % 10);
        Cat.getProducer().newEvent("SQL.Method", "Delete").setStatus(Message.SUCCESS);
        Cat.getProducer().newEvent("SQL.Database", "jdbc:mysql://192.168.7.43:3306/database" + k)
              .setStatus(Message.SUCCESS);
        t3.addData("delete * from hostinfo");
        t3.setStatus(Message.SUCCESS);
        Cat.getManager().getThreadLocalMessageTree().setDomain("CatDemo");
        t3.complete();
      }
    }
    Thread.sleep(1000);
  }
View Full Code Here

  @Test
  public void sendDependencyTransaction() throws Exception {
    int size = 10;
    for (int i = 0; i < size; i++) {
      Transaction t = Cat.getProducer().newTransaction("SQL", "User.select" + i % 10);
      Cat.getProducer().newEvent("SQL.Method", "Select").setStatus(Message.SUCCESS);
      Cat.getProducer().newEvent("SQL.Database", "jdbc:mysql://192.168.7.43:3306/database" + i % 4)
            .setStatus(Message.SUCCESS);
      t.addData("select * from hostinfo");
      t.setStatus(Message.SUCCESS);
      t.complete();
    }

    for (int i = 0; i < size; i++) {
      Transaction t = Cat.getProducer().newTransaction("PigeonCall", "Method3");
      Cat.getProducer().newEvent("PigeonCall.server", "192.168.64.11:2280");
      t.addData("key and value");

      Thread.sleep(1);
      t.complete();
    }

    for (int i = 0; i < size; i++) {
      Transaction t = Cat.getProducer().newTransaction("PigeonService", "Method6");
      Cat.getProducer().newEvent("PigeonService.client", "192.168.7.77");
      t.addData("key and value");

      Thread.sleep(51);
      t.complete();
    }
  }
View Full Code Here

 
  @Test
  public void sendTraceInfo() throws Exception {

    for (int i = 0; i < 10; i++) {
      Transaction t = Cat.newTransaction("Trace", "Test"+i);
      try {
        Cat.logTrace("Trace", "Info");
        Cat.logTrace("Trace", "Dubug", Trace.SUCCESS, "sss");
        Trace trace = Cat.newTrace("Trace", "Error");

        trace.setStatus(Trace.SUCCESS);
        trace.addData("errorTrace");
        t.setStatus("Fail");
      } catch (Exception e) {
        t.setStatus(Transaction.SUCCESS);
        Cat.logError(e);
        throw e;
      } finally {
        t.complete();
      }
    }
    Thread.sleep(10000);
  }
View Full Code Here

public class TestMaxMessage {

  @Test
  public void testSend() throws Exception {
    for (int i = 0; i < 10000; i++) {
      Transaction t = Cat.getProducer().newTransaction("CatTest", "CatTest" + i % 10);
      t.setStatus(Message.SUCCESS);
      Cat.getProducer().newEvent("Cache.kvdb", "Method" + i % 10 + ":missed");
      Cat.logError(new NullPointerException());
      t.addData("key and value");
      t.complete();
    }
    Thread.sleep(10 * 1000);
  }
 
View Full Code Here

    long time = System.currentTimeMillis();
    int i = 10;

    while (i > 0) {
      i++;
      Transaction total = Cat.newTransaction("Test", "Test");
      Transaction t = Cat.getProducer().newTransaction("Cache.kvdb", "Method" + i % 10);
      t.setStatus(Message.SUCCESS);
      Cat.getProducer().newEvent("Cache.kvdb", "Method" + i % 10 + ":missed");
      t.addData("key and value");

      Transaction t2 = Cat.getProducer().newTransaction("Cache.web", "Method" + i % 10);
      Cat.getProducer().newEvent("Cache.web", "Method" + i % 10 + ":missed");
      t2.addData("key and value");
      t2.setStatus(Message.SUCCESS);
      t2.complete();

      Transaction t3 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t3.addData("key and value");
      t3.setStatus(Message.SUCCESS);
      t3.complete();

      Transaction t4 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t4.addData("key and value");
      t4.setStatus(Message.SUCCESS);
      t4.complete();

      Transaction t5 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      Transaction t6 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t6.addData("key and value");
      t6.setStatus(Message.SUCCESS);
      t6.complete();

      Transaction t9 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      Transaction t7 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t7.addData("key and value");
      t7.setStatus(Message.SUCCESS);
      t7.complete();

      Transaction t8 = Cat.getProducer().newTransaction("Cache.memcached", "Method" + i % 10);
      t8.addData("key and value");
      t8.setStatus(Message.SUCCESS);
      t8.complete();

      t9.addData("key and value");
      t9.setStatus(Message.SUCCESS);
      t9.complete();
View Full Code Here

    loadHourlyReports(currentStartTime - Constants.HOUR, StoragePolicy.FILE);
  }

  @Override
  public Map<String, T> loadHourlyReports(long startTime, StoragePolicy policy) {
    Transaction t = Cat.newTransaction("Restore", m_name);
    Map<String, T> reports = m_reports.get(startTime);
    Bucket<String> bucket = null;

    if (reports == null) {
      reports = new ConcurrentHashMap<String, T>();
      m_reports.put(startTime, reports);
    }

    try {
      bucket = m_bucketManager.getReportBucket(startTime, m_name);

      for (String id : bucket.getIds()) {
        String xml = bucket.findById(id);
        T report = m_reportDelegate.parseXml(xml);

        reports.put(id, report);
      }

      m_reportDelegate.afterLoad(reports);
      t.setStatus(Message.SUCCESS);
    } catch (Throwable e) {
      t.setStatus(e);
      Cat.logError(e);
      m_logger.error(String.format("Error when loading %s reports of %s!", m_name, new Date(startTime)), e);
    } finally {
      t.complete();

      if (bucket != null) {
        m_bucketManager.closeBucket(bucket);
      }
    }
View Full Code Here

    m_reportDelegate = reportDelegate;
  }

  @Override
  public void storeHourlyReports(long startTime, StoragePolicy policy) {
    Transaction t = Cat.newTransaction("Checkpoint", m_name);
    Map<String, T> reports = m_reports.get(startTime);
    Bucket<String> bucket = null;

    try {
      t.addData("reports", reports == null ? 0 : reports.size());

      if (reports != null) {
        m_reportDelegate.beforeSave(reports);

        if (policy.forFile()) {
          bucket = m_bucketManager.getReportBucket(startTime, m_name);

          try {
            for (T report : reports.values()) {
              try {
                String domain = m_reportDelegate.getDomain(report);
                String xml = m_reportDelegate.buildXml(report);

                bucket.storeById(domain, xml);
              } catch (Exception e) {
                t.setStatus(e);
                Cat.logError(e);
              }
            }
          } finally {
            m_bucketManager.closeBucket(bucket);
          }
        }

        if (policy.forDatabase()) {
          Date period = new Date(startTime);
          String ip = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();

          for (T report : reports.values()) {
            try {
              String domain = m_reportDelegate.getDomain(report);
              HourlyReport r = m_reportDao.createLocal();

              r.setName(m_name);
              r.setDomain(domain);
              r.setPeriod(period);
              r.setIp(ip);
              r.setType(1);
              r.setContent("");

              m_reportDao.insert(r);

              int id = r.getId();
              byte[] binaryContent = m_reportDelegate.buildBinary(report);
              HourlyReportContent content = m_reportContentDao.createLocal();

              content.setReportId(id);
              content.setContent(binaryContent);
              m_reportContentDao.insert(content);
              m_reportDelegate.createHourlyTask(report);
            } catch (Throwable e) {
              t.setStatus(e);
              Cat.getProducer().logError(e);
            }
          }
        }
      }
      t.setStatus(Message.SUCCESS);
    } catch (Throwable e) {
      Cat.logError(e);
      t.setStatus(e);
      m_logger.error(String.format("Error when storing %s reports of %s!", m_name, new Date(startTime)), e);
    } finally {
      cleanup();
      t.complete();

      if (bucket != null) {
        m_bucketManager.closeBucket(bucket);
      }
    }
View Full Code Here

TOP

Related Classes of com.dianping.cat.message.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.