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

S
superjom 已提交
4 5
#include <glog/logging.h>
#include <memory>
S
Superjom 已提交
6
#include <mutex>
S
superjom 已提交
7 8
#include <string>

S
superjom 已提交
9 10
#include "visualdl/storage/storage.h"
#include "visualdl/utils/concurrency.h"
S
superjom 已提交
11 12 13 14

namespace visualdl {

/*
S
superjom 已提交
15
 * IM(Information Maintainer) maintain the Storage singleton in memory,
S
superjom 已提交
16 17 18 19 20 21 22 23 24 25 26 27
 * 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 已提交
28
 */
S
superjom 已提交
29
class IM final {
S
superjom 已提交
30
public:
S
Superjom 已提交
31 32
  IM() { storage_.reset(new MemoryStorage(&executor_)); }
  // IM(StorageBase::Type type, StorageBase::Mode mode);
S
Superjom 已提交
33
  ~IM() { executor_.Quit(); }
S
superjom 已提交
34

S
Superjom 已提交
35
  void MaintainRead(const std::string &dir, int msecs) {
S
superjom 已提交
36
    LOG(INFO) << "start maintain read";
S
Superjom 已提交
37 38
    dynamic_cast<MemoryStorage *>(storage_.get())
        ->StartReadService(dir, msecs, &lock_);
S
superjom 已提交
39 40
  }

S
Superjom 已提交
41 42
  void MaintainWrite(const std::string &dir, int msecs) {
    dynamic_cast<MemoryStorage *>(storage_.get())
S
superjom 已提交
43
        ->StartWriteService(dir, msecs, &lock_);
S
superjom 已提交
44 45 46 47 48 49 50 51 52 53 54
  }

  /*
   * 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 已提交
55
   * @record: a record
S
superjom 已提交
56 57 58 59
   *
   * NOTE pass in the serialized protobuf message will trigger copying, but
   * simpler to support different Tablet data formats.
   */
S
superjom 已提交
60
  void AddRecord(const std::string &tag, const storage::Record &record);
S
superjom 已提交
61

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

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

S
superjom 已提交
72
  StorageBase &storage() { return *storage_; }
S
superjom 已提交
73

S
Superjom 已提交
74 75 76 77
  cc::PeriodExector &executor() { return executor_; }

  std::mutex &handler() { return lock_; }

S
superjom 已提交
78
private:
S
Superjom 已提交
79
  // read write lock for protobuf in memory
S
superjom 已提交
80 81
  // TODO(ChunweiYan) mutex too heavy here, might change to a message queue to
  // reduce the frequency of visiting disk
S
Superjom 已提交
82
  std::mutex lock_;
S
superjom 已提交
83
  std::unique_ptr<StorageBase> storage_;
S
Superjom 已提交
84
  cc::PeriodExector executor_;
S
superjom 已提交
85 86
};

S
superjom 已提交
87
}  // namespace visualdl
S
superjom 已提交
88

S
superjom 已提交
89
#endif  // VISUALDL_BACKEND_LOGIC_IM_H