Ceph Node.js Bindings for Librados

2014年8月25日 laurentbarbe
1
2
3
4
5
6
7
8
9
var cluster = new rados.Rados( "ceph", "client.admin", "/etc/ceph/ceph.conf");
cluster.connect();

var ioctx = new rados.Ioctx(cluster, "data");
ioctx.aio_write("testfile2", new Buffer("1234567879ABCD"), 14, 0, function (err) {
  if (err) {
    throw err;
  }
  ...

据我所知,目前还没有librados的Node.js封装。(我想亚历山大还没有找到他开始编写的代码。http://tracker.ceph.com/issues/4230)。所以,我开始编写一个插件的草稿(当我有一些时间的时候)。现在我还没有使用它,但它让我能够学习Node. 如果有人感兴趣,可以在这里找到:

https://github.com/ksperis/node-rados

(欢迎提出建议,尤其是在错误处理、libuv的使用、缓冲区/字符串以及其他方面……)

并非所有功能都已实现,但基本功能已经存在。

例如(repo中的example.js文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var rados = require('./build/Release/rados');

// EXEMPLE FILE

//==================================
//     Connect to cluster
//==================================
var cluster = new rados.Rados( "ceph", "client.admin", "/etc/ceph/ceph.conf");

var err = cluster.connect();
if (err) {
  // On connection error
  console.log("Error " + err);
  throw err;
}

// Print cluster FSID, pools
console.log( "fsid : " + cluster.get_fsid() );
console.log( "ls pools : " + cluster.pool_list() );


//==================================
//     Create IOCTX
//==================================
var ioctx = new rados.Ioctx(cluster, "data");

console.log(" --- RUN Sync Write / Read --- ");
// Sync write_full
ioctx.write_full("testfile1", new Buffer("01234567ABCDEF"));

// Sync Read
console.log( "Read data : " +
  ioctx.read("testfile1", ioctx.stat("testfile1").psize).toString() );

// Remove
ioctx.remove("testfile1");

console.log(" --- RUN ASync Write / Read --- ");
// ASync write_full
ioctx.aio_write("testfile2", new Buffer("1234567879ABCD"), 14, 0, function (err) {
  if (err) {
    throw err;
  }

  ioctx.aio_read("testfile2", 14, 0, function (err, data) {
  if (err) {
    throw err;
  }

   console.log("[async callback] data = " + data.toString());

  });

});


//==================================
//     Read / Write Attributes
//==================================

console.log(" --- RUN Attributes Write / Read --- ");

ioctx.setxattr("testfile3", "attr1", "first attr");
ioctx.setxattr("testfile3", "attr2", "second attr");
ioctx.setxattr("testfile3", "attr3", "last attr value");

var attrs = ioctx.getxattrs("testfile3");

console.log("testfile3 xattr = %j", attrs);


// destroy ioctx and close cluster after aio_flush
ioctx.aio_flush_async(function (err) {
  ioctx.destroy();
  cluster.shutdown();
});


process.exit(code=0)

// OTHER EXEMPLES

//   Read Sync file in chunks
var file = "testfile";
var   fileSize = ioctx.stat(file).psize,
  chunkSize = 512,
    bytesRead = 0;


while (bytesRead < fileSize) {
    if ((bytesRead + chunkSize) > fileSize) {
        chunkSize = (fileSize - bytesRead);
    }
    var buffer = ioctx.read(file, chunkSize, bytesRead);
    bytesRead += chunkSize;
    process.stdout.write(buffer.toString());
}


//   Read Async file in chunks
var file = "testfile";
var   fileSize = ioctx.stat(file).psize,
  chunkSize = 512,
    bytesRead = 0;


while (bytesRead < fileSize) {
    if ((bytesRead + chunkSize) > fileSize) {
        chunkSize = (fileSize - bytesRead);
    }
    ioctx.aio_read(file, chunkSize, bytesRead, function (err, data) {
      process.stdout.write(data.toString());
    });
    bytesRead += chunkSize;
}


//   Use snapshot
ioctx.write_full("testfile10", new Buffer("version1"));
ioctx.snap_create("snaptest1");
ioctx.write_full("testfile10", new Buffer("version2"));
ioctx.snap_create("snaptest2");
ioctx.write_full("testfile10", new Buffer("version3"));
ioctx.snap_create("snaptest3");

ioctx.snap_rollback("testfile10", "snaptest2");
console.log(ioctx.read("testfile10").toString());

ioctx.snap_remove("snaptest1");
ioctx.snap_remove("snaptest2");
ioctx.snap_remove("snaptest3");