im.h 2.1 KB
Newer Older
S
superjom 已提交
1 2 3
#ifndef VISUALDL_BACKEND_LOGIC_IM_H
#define VISUALDL_BACKEND_LOGIC_IM_H

S
superjom 已提交
4
#include <glog/logging.h>
S
superjom 已提交
5
#include <visualdl/backend/utils/concurrency.h>
S
superjom 已提交
6
#include <memory>
S
superjom 已提交
7 8 9 10 11 12 13
#include <string>

#include "visualdl/backend/storage/storage.h"

namespace visualdl {

/*
S
superjom 已提交
14
 * IM(Information Maintainer) maintain the Storage singleton in memory,
S
superjom 已提交
15 16 17 18 19 20 21 22 23 24 25 26
 * pre-compute some the statistical information to help visualizaton.
 *
 * There should be two processes and each have an IM, one is the web server
 * which hold one IM to read the storage, the other is the SDK(python or C++),
 * it will get an IM to write latest changes to storage.
 *
 * An IM have an underlying Storage object, which might be a memory based
 * storage or a disk based one, both has the same interfaces those defined by
 * class StorageBase.
 *
 * The SDK's IM will maintain the changes and periodically write to disk, and
 * the web server's IM will periodically read latest storage from disk.
S
superjom 已提交
27
 */
S
superjom 已提交
28
class IM final {
S
superjom 已提交
29
public:
S
superjom 已提交
30 31 32
  IM() { storage_.reset(new MemoryStorage); }
  IM(StorageBase::Type type, StorageBase::Mode mode);
  ~IM() { cc::PeriodExector::Global().Quit(); }
S
superjom 已提交
33

S
superjom 已提交
34
  static IM &Global() {
S
superjom 已提交
35 36 37 38 39 40 41 42 43 44 45
    static IM x;
    return x;
  }

  void MaintainRead() {
    LOG(INFO) << "start maintain read";
    dynamic_cast<MemoryStorage *>(storage_.get())->StartReadService();
  }

  void MaintainWrite() {
    dynamic_cast<MemoryStorage *>(storage_.get())->StartWriteSerice();
S
superjom 已提交
46 47 48 49 50 51 52 53 54 55 56
  }

  /*
   * Set the disk path to store the Storage object.
   */
  void SetPersistDest(const std::string &path);

  storage::Tablet *AddTablet(const std::string &tag, int num_samples);

  /*
   * @tag: tag of the target Tablet.
S
superjom 已提交
57
   * @record: a record
S
superjom 已提交
58 59 60 61
   *
   * NOTE pass in the serialized protobuf message will trigger copying, but
   * simpler to support different Tablet data formats.
   */
S
superjom 已提交
62
  void AddRecord(const std::string &tag, const storage::Record &record);
S
superjom 已提交
63

S
superjom 已提交
64 65 66 67 68
  /*
   * delete all the information.
   */
  void Clear();

S
superjom 已提交
69 70 71 72 73
  /*
   * Save the Storage Protobuf to disk.
   */
  void PersistToDisk();

S
superjom 已提交
74
  StorageBase &storage() { return *storage_; }
S
superjom 已提交
75 76

private:
S
superjom 已提交
77
  std::unique_ptr<StorageBase> storage_;
S
superjom 已提交
78 79
};

S
superjom 已提交
80
}  // namespace visualdl
S
superjom 已提交
81

S
superjom 已提交
82
#endif  // VISUALDL_BACKEND_LOGIC_IM_H