Skip to content

API介绍与使用

flabby edited this page May 27, 2017 · 1 revision

API使用

API

  • 一致性接口:
    • Read
    • Write
    • Delete
  • 本地接口:
    • DirtyWrite
    • DirtyRead
  • 查询接口:
    • GetLeader
    • GetServerStatus
  • 调试接口
    • set_log_level

使用

  • 使用Floyd步骤
    • 1 设置FloydOptions
    • 2 Floyd::Open()
    • 3 floyd->Start()
    • 4 调用API
#include <iostream>
#include <string>
#include <unistd.h>

#include "floyd/include/floyd_options.h"
#include "floyd/include/floyd.h"

#include "slash/include/slash_status.h"

int main() {

  // Step 0: set options;

  floyd::Options options;
  // A cluster of 3 servers;
  options.SetMembers("127.0.0.1:8900;127.0.0.1:8901:127.0.0.1:8902");
  //  Server 0 should use this ip:port
  options.local_ip = "127.0.0.1";
  options.local_port = 8900;
  options.data_path = "data/path0/";
  options.log_path = "log/path0/";

  //  Server 1 should use this ip:port
  //options.local_ip = "127.0.0.1"
  //options.local_port = 8901;
  //options.data_path = "data/path1/";
  //options.log_path = "log/path1/";

  //  Server 2 should use this ip:port
  //options.local_ip = "127.0.0.1"
  //options.local_port = 8902;
  //options.data_path = "data/path2/";
  //options.log_path = "log/path2/";

  options.Dump();


  // Step 1: Open Floyd
  floyd::Floyd *floyd;
  floyd::Floyd::Open(options, &floyd);

  // Step 2: Start Floyd
  slash::Status s = floyd->Start();
  if (!s.ok()) {
    printf ("Open Floyd failed %s\n", s.ToString().c_str());
    return -1;
  }

  // Wait for leader by GetLeader API
  std::string leader_ip;
  int leader_port;
  while (!floyd->GetLeader(&leader_ip, &leader_port)) {
    printf ("Wait leader ...\n");
    // Wait leader election
    sleep(1);
  }


  // Write
  slash::Status result;
  result = floyd->Write("test_key", "test_val");
  if (!result.ok()) {
    printf ("Write failed %s\n", result.ToString().c_str());
  } else {
    printf ("Write key ok!");
  }

  // Read
  std::string value;
  result = floyd->Read("test_key", value);
  if (result.ok()) {
    printf ("Read key ok, value is %s\n", value.c_str());
  } else if (result.IsNotFound()) {
    printf ("Read key notfound\n");
  } else {
    printf ("Read failed %s\n", result.ToString().c_str());
  }

  delete floyd;

  return 0;
}
  • 可以参考example/server中使用Floyd、Pink构建节点FloydServer的代码;
  • 对应的example/sdk为客户端代码;

简介

安装使用

设计与实现

工具

  • FileLog解析工具log_parser

测试

Clone this wiki locally