Package com.dianping.cat.message

Examples of com.dianping.cat.message.Transaction


  private long m_sleepPeriod = 1000L * 60;

  private volatile boolean m_active = true;

  public void deleteOldReports() {
    Transaction t = Cat.newTransaction("System", "DeleteReport");
    try {
      File reportDir = new File(m_reportBaseDir);
      final List<String> toRemovePaths = new ArrayList<String>();
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
      final String today = sdf.format(date);
      final String yesterday = sdf.format(new Date(date.getTime() - 24 * 60 * 60 * 1000L));

      Scanners.forDir().scan(reportDir, new FileMatcher() {
        @Override
        public Direction matches(File base, String path) {
          File file = new File(base, path);
          if (file.isFile() && shouldDeleteReport(path)) {
            toRemovePaths.add(path);
          }
          return Direction.DOWN;
        }

        private boolean shouldDeleteReport(String path) {
          if (path.indexOf(today) > -1 || path.indexOf(yesterday) > -1) {
            return false;
          } else {
            return true;
          }
        }
      });
      for (String path : toRemovePaths) {
        File file = new File(m_reportBaseDir, path);

        file.delete();
        Cat.logEvent("System", "DeleteReport", Event.SUCCESS, file.getAbsolutePath());
      }
      removeEmptyDir(reportDir);
      t.setStatus(Transaction.SUCCESS);
    } catch (Exception e) {
      Cat.logError(e);
      t.setStatus(e);
    } finally {
      t.complete();
    }
  }
View Full Code Here


    if (len > 0) {
      Cat.setup("DumpUploader");

      MessageProducer cat = Cat.getProducer();
      String ip = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
      Transaction root = cat.newTransaction("System", "Dump-" + ip);

      Collections.sort(paths);

      root.addData("files", paths);
      root.setStatus(Message.SUCCESS);

      for (int i = 0; i < len; i++) {
        String path = paths.get(i);
        Transaction t = cat.newTransaction("System", "UploadDump");
        File file = new File(baseDir, path);

        t.addData("file", path);

        FSDataOutputStream fdos = null;
        FileInputStream fis = null;
        try {
          fdos = makeHdfsOutputStream(path);
          fis = new FileInputStream(file);

          long start = System.currentTimeMillis();

          Files.forIO().copy(fis, fdos, AutoClose.INPUT_OUTPUT);

          double sec = (System.currentTimeMillis() - start) / 1000d;
          String size = Formats.forNumber().format(file.length(), "0.#", "B");
          String speed = sec <= 0 ? "N/A" : Formats.forNumber().format(file.length() / sec, "0.0", "B/s");

          t.addData("size", size);
          t.addData("speed", speed);
          t.setStatus(Message.SUCCESS);

          if (!file.delete()) {
            m_logger.warn("Can't delete file: " + file);
          }
        } catch (AlreadyBeingCreatedException e) {
          Cat.logError(e);
          t.setStatus(e);

          m_logger.error(String.format("Already being created (%s)!", path), e);
        } catch (AccessControlException e) {
          cat.logError(e);
          t.setStatus(e);
          m_logger.error(String.format("No permission to create HDFS file(%s)!", path), e);
        } catch (Exception e) {
          cat.logError(e);
          t.setStatus(e);
          m_logger.error(String.format("Uploading file(%s) to HDFS(%s) failed!", file, path), e);
        } finally {
          try {
            if (fdos != null) {
              fdos.close();
            }
          } catch (IOException e) {
            Cat.logError(e);
          }
          t.complete();
        }

        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
View Full Code Here

    if (!m_serverConfigManager.isHdfsOn()) {
      return null;
    }

    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("BucketService", getClass().getSimpleName());

    t.setStatus(Message.SUCCESS);

    try {
      MessageId id = MessageId.parse(messageId);
      final String path = m_pathBuilder.getPath(new Date(id.getTimestamp()), "");
      final StringBuilder sb = new StringBuilder();
      FileSystem fs = m_manager.getFileSystem("dump", sb);

      sb.append('/').append(path);

      final String key = id.getDomain() + '-' + id.getIpAddress();
      final String str = sb.toString();
      final Path basePath = new Path(str);
      final List<String> paths = new ArrayList<String>();

      fs.listStatus(basePath, new PathFilter() {
        @Override
        public boolean accept(Path p) {
          String name = p.getName();

          if (name.contains(key) && !name.endsWith(".idx")) {
            paths.add(path + name);
          }
          return false;
        }
      });

      t.addData(paths.toString());
      for (String dataFile : paths) {
        try {
          Cat.getProducer().logEvent("HDFSBucket", dataFile);
          HdfsMessageBucket bucket = m_buckets.get(dataFile);

          if (bucket == null) {
            bucket = (HdfsMessageBucket) lookup(MessageBucket.class, HdfsMessageBucket.ID);
            bucket.initialize(dataFile);
            m_buckets.put(dataFile, bucket);
          }
          if (bucket != null) {
            MessageTree tree = bucket.findById(messageId);

            if (tree != null && tree.getMessageId().equals(messageId)) {
              t.addData("path", dataFile);
              return tree;
            }
          }
        } catch (Exception e) {
          t.setStatus(e);
          Cat.logError(e);
        }
      }

      return null;
    } catch (IOException e) {
      t.setStatus(e);
      cat.logError(e);
      throw e;
    } catch (RuntimeException e) {
      t.setStatus(e);
      cat.logError(e);
      throw e;
    } finally {
      t.complete();
    }
  }
View Full Code Here

  @Override
  public void run() {
    boolean active = true;

    while (active) {
      Transaction t = Cat.newTransaction("Agent", "Executors");

      try {
        long current = System.currentTimeMillis();

        for (Executor executor : m_executors) {
          Transaction t2 = Cat.newTransaction("Executor", executor.getId());
          try {
            List<DataEntity> entities = executor.execute();

            m_sender.put(entities);
            t2.setStatus(Transaction.SUCCESS);
          } catch (Exception e) {
            t2.setStatus(e);
            Cat.logError(e);
          } finally {
            t2.complete();
          }
        }
        t.setStatus(Transaction.SUCCESS);

        long duration = System.currentTimeMillis() - current;
View Full Code Here

public class CatEnvironmentTest {
  @Test
  public void testWithoutInitialize() throws InterruptedException {
    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("TestType", "TestName");

    t.addData("data here");
    t.setStatus("TestStatus");
    t.complete();

    Thread.sleep(100);
    Assert.assertEquals(true, Cat.isInitialized());
    Cat.destroy();
  }
View Full Code Here

  @Test
  public void testWithInitialize() throws InterruptedException {
    Cat.initialize(new File("/data/appdatas/cat/client.xml"));
    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("TestType", "TestName");

    t.addData("data here");
    t.setStatus("TestStatus");
    t.complete();

    Thread.sleep(100);

    Assert.assertEquals(true, Cat.isInitialized());
    Cat.destroy();
View Full Code Here

  @Test
  public void testWithNoExistGlobalConfigInitialize() throws InterruptedException {
    Cat.initialize(new File("/data/appdatas/cat/clientNoExist.xml"));
    MessageProducer cat = Cat.getProducer();
    Transaction t = cat.newTransaction("TestType", "TestName");

    t.addData("data here");
    t.setStatus("TestStatus");
    t.complete();

    Thread.sleep(100);

    Assert.assertEquals(true, Cat.isInitialized());
    Cat.destroy();
View Full Code Here

 
 
  @Test
  public void testJobTest() throws Exception{
    Cat.initialize("192.168.7.70","192.168.7.71");
    Transaction t = Cat.newTransaction("TestType", "TestName");

    t.addData("data here");
    t.setStatus("TestStatus");
    t.complete();

    Thread.sleep(10000);
  }
View Full Code Here

  private AppDataComparisonRender m_render;

  private SimpleDateFormat m_sdf = new SimpleDateFormat("yyyy-MM-dd");

  public void doNotifying(Date period) {
    Transaction t = Cat.newTransaction("AppDataComparitonNotifier", m_sdf.format(period));

    try {
      Map<String, AppDataComparisonResult> results = buildAppDataComparisonResults(period,
            m_appComparisonConfigManager.getConfig());
      Map<List<String>, List<AppDataComparisonResult>> results2Receivers = buildReceivers2Results(results);

      for (Entry<List<String>, List<AppDataComparisonResult>> entry : results2Receivers.entrySet()) {
        notify(period, entry.getValue(), entry.getKey());
      }

      t.setStatus(Transaction.SUCCESS);
    } catch (Exception e) {
      t.setStatus(e);
    } finally {
      t.complete();
    }
  }
View Full Code Here

      return null;
    }
  }

  private String queryDomainFromCMDB(String ip) {
    Transaction t = Cat.newTransaction("CMDB", "queryDomain");

    try {
      String cmdb = String.format(CMDB_DOMAIN_URL, ip);
      InputStream in = Urls.forIO().readTimeout(1000).connectTimeout(1000).openStream(cmdb);
      String content = Files.forIO().readFrom(in, "utf-8");

      t.setStatus(Transaction.SUCCESS);
      t.addData(content);
      return parseDomain(content.trim());
    } catch (Exception e) {
      Cat.logError(e);
      t.setStatus(e);
    } finally {
      t.complete();
    }
    return null;
  }
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.