libcouchbase 1.2.8 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -4
  3. data/README.md +16 -8
  4. data/ext/libcouchbase/CMakeLists.txt +34 -32
  5. data/ext/libcouchbase/RELEASE_NOTES.markdown +277 -6
  6. data/ext/libcouchbase/cmake/Modules/ConfigureDtrace.cmake +14 -0
  7. data/ext/libcouchbase/cmake/Modules/FindCouchbaseLibevent.cmake +2 -0
  8. data/ext/libcouchbase/cmake/Modules/FindCouchbaseLibuv.cmake +2 -1
  9. data/ext/libcouchbase/cmake/Modules/GenerateConfigDotH.cmake +2 -0
  10. data/ext/libcouchbase/cmake/Modules/GetLibcouchbaseFlags.cmake +8 -1
  11. data/ext/libcouchbase/cmake/Modules/GetVersionInfo.cmake +3 -3
  12. data/ext/libcouchbase/cmake/config-cmake.h.in +14 -0
  13. data/ext/libcouchbase/cmake/configure +8 -26
  14. data/ext/libcouchbase/cmake/defs.mk.in +2 -2
  15. data/ext/libcouchbase/cmake/libcouchbase.stp.in +829 -0
  16. data/ext/libcouchbase/cmake/source_files.cmake +11 -2
  17. data/ext/libcouchbase/contrib/cbsasl/CMakeLists.txt +18 -2
  18. data/ext/libcouchbase/contrib/cbsasl/include/cbsasl/cbsasl.h +44 -2
  19. data/ext/libcouchbase/contrib/cbsasl/src/client.c +285 -73
  20. data/ext/libcouchbase/contrib/cbsasl/src/common.c +4 -0
  21. data/ext/libcouchbase/contrib/cbsasl/src/scram-sha/scram_utils.c +500 -0
  22. data/ext/libcouchbase/contrib/cbsasl/src/scram-sha/scram_utils.h +99 -0
  23. data/ext/libcouchbase/contrib/cliopts/CMakeLists.txt +1 -1
  24. data/ext/libcouchbase/contrib/cliopts/cliopts.h +14 -1
  25. data/ext/libcouchbase/contrib/snappy/CMakeLists.txt +2 -3
  26. data/ext/libcouchbase/contrib/snappy/snappy-sinksource.cc +4 -0
  27. data/ext/libcouchbase/contrib/snappy/snappy-stubs-public.h +7 -5
  28. data/ext/libcouchbase/contrib/snappy/snappy.cc +7 -2
  29. data/ext/libcouchbase/example/crypto/.gitignore +2 -0
  30. data/ext/libcouchbase/example/crypto/Makefile +13 -0
  31. data/ext/libcouchbase/example/crypto/common_provider.c +24 -0
  32. data/ext/libcouchbase/example/crypto/common_provider.h +31 -0
  33. data/ext/libcouchbase/example/crypto/openssl_symmetric_decrypt.c +139 -0
  34. data/ext/libcouchbase/example/crypto/openssl_symmetric_encrypt.c +147 -0
  35. data/ext/libcouchbase/example/crypto/openssl_symmetric_provider.c +281 -0
  36. data/ext/libcouchbase/example/crypto/openssl_symmetric_provider.h +29 -0
  37. data/ext/libcouchbase/example/tracing/.gitignore +2 -0
  38. data/ext/libcouchbase/example/tracing/Makefile +8 -0
  39. data/ext/libcouchbase/example/tracing/cJSON.c +1 -0
  40. data/ext/libcouchbase/example/tracing/cJSON.h +1 -0
  41. data/ext/libcouchbase/example/tracing/tracing.c +439 -0
  42. data/ext/libcouchbase/example/tracing/views.c +444 -0
  43. data/ext/libcouchbase/include/libcouchbase/auth.h +56 -4
  44. data/ext/libcouchbase/include/libcouchbase/cbft.h +8 -0
  45. data/ext/libcouchbase/include/libcouchbase/cntl-private.h +55 -1
  46. data/ext/libcouchbase/include/libcouchbase/cntl.h +101 -1
  47. data/ext/libcouchbase/include/libcouchbase/configuration.h.in +6 -0
  48. data/ext/libcouchbase/include/libcouchbase/couchbase.h +109 -6
  49. data/ext/libcouchbase/include/libcouchbase/crypto.h +140 -0
  50. data/ext/libcouchbase/include/libcouchbase/error.h +38 -2
  51. data/ext/libcouchbase/include/libcouchbase/kvbuf.h +6 -1
  52. data/ext/libcouchbase/include/libcouchbase/metrics.h +79 -0
  53. data/ext/libcouchbase/include/libcouchbase/n1ql.h +9 -0
  54. data/ext/libcouchbase/include/libcouchbase/tracing.h +319 -0
  55. data/ext/libcouchbase/include/libcouchbase/vbucket.h +1 -1
  56. data/ext/libcouchbase/include/libcouchbase/views.h +8 -0
  57. data/ext/libcouchbase/include/memcached/protocol_binary.h +40 -10
  58. data/ext/libcouchbase/packaging/rpm/libcouchbase.spec.in +6 -14
  59. data/ext/libcouchbase/plugins/io/libuv/plugin-internal.h +3 -0
  60. data/ext/libcouchbase/plugins/io/libuv/plugin-libuv.c +1 -0
  61. data/ext/libcouchbase/plugins/io/select/plugin-select.c +4 -1
  62. data/ext/libcouchbase/src/auth-priv.h +36 -4
  63. data/ext/libcouchbase/src/auth.cc +66 -27
  64. data/ext/libcouchbase/src/bootstrap.cc +1 -1
  65. data/ext/libcouchbase/src/bucketconfig/bc_cccp.cc +12 -7
  66. data/ext/libcouchbase/src/bucketconfig/bc_http.cc +26 -17
  67. data/ext/libcouchbase/src/bucketconfig/bc_http.h +1 -1
  68. data/ext/libcouchbase/src/bucketconfig/clconfig.h +4 -2
  69. data/ext/libcouchbase/src/bucketconfig/confmon.cc +6 -3
  70. data/ext/libcouchbase/src/cbft.cc +48 -0
  71. data/ext/libcouchbase/src/cntl.cc +138 -2
  72. data/ext/libcouchbase/src/config_static.h +17 -0
  73. data/ext/libcouchbase/src/connspec.cc +54 -6
  74. data/ext/libcouchbase/src/connspec.h +9 -1
  75. data/ext/libcouchbase/src/crypto.cc +386 -0
  76. data/ext/libcouchbase/src/ctx-log-inl.h +23 -6
  77. data/ext/libcouchbase/src/dump.cc +4 -0
  78. data/ext/libcouchbase/src/getconfig.cc +1 -2
  79. data/ext/libcouchbase/src/handler.cc +65 -27
  80. data/ext/libcouchbase/src/hostlist.cc +35 -7
  81. data/ext/libcouchbase/src/hostlist.h +7 -0
  82. data/ext/libcouchbase/src/http/http-priv.h +2 -0
  83. data/ext/libcouchbase/src/http/http.cc +77 -37
  84. data/ext/libcouchbase/src/http/http_io.cc +19 -2
  85. data/ext/libcouchbase/src/instance.cc +90 -17
  86. data/ext/libcouchbase/src/internal.h +5 -0
  87. data/ext/libcouchbase/src/lcbio/connect.cc +39 -4
  88. data/ext/libcouchbase/src/lcbio/connect.h +27 -0
  89. data/ext/libcouchbase/src/lcbio/ctx.c +49 -23
  90. data/ext/libcouchbase/src/lcbio/ioutils.cc +30 -3
  91. data/ext/libcouchbase/src/lcbio/ioutils.h +2 -0
  92. data/ext/libcouchbase/src/lcbio/manager.cc +44 -8
  93. data/ext/libcouchbase/src/lcbio/manager.h +2 -0
  94. data/ext/libcouchbase/src/lcbio/rw-inl.h +1 -0
  95. data/ext/libcouchbase/src/lcbio/ssl.h +3 -5
  96. data/ext/libcouchbase/src/logging.c +1 -1
  97. data/ext/libcouchbase/src/logging.h +2 -0
  98. data/ext/libcouchbase/src/mc/compress.cc +164 -0
  99. data/ext/libcouchbase/src/mc/compress.h +7 -12
  100. data/ext/libcouchbase/src/mc/mcreq-flush-inl.h +5 -1
  101. data/ext/libcouchbase/src/mc/mcreq.c +11 -1
  102. data/ext/libcouchbase/src/mc/mcreq.h +35 -4
  103. data/ext/libcouchbase/src/mcserver/mcserver.cc +30 -7
  104. data/ext/libcouchbase/src/mcserver/mcserver.h +7 -0
  105. data/ext/libcouchbase/src/mcserver/negotiate.cc +103 -57
  106. data/ext/libcouchbase/src/mcserver/negotiate.h +2 -2
  107. data/ext/libcouchbase/src/mctx-helper.h +11 -0
  108. data/ext/libcouchbase/src/metrics.cc +132 -0
  109. data/ext/libcouchbase/src/n1ql/ixmgmt.cc +2 -1
  110. data/ext/libcouchbase/src/n1ql/n1ql.cc +66 -0
  111. data/ext/libcouchbase/src/newconfig.cc +9 -2
  112. data/ext/libcouchbase/src/operations/counter.cc +2 -1
  113. data/ext/libcouchbase/src/operations/durability-cas.cc +11 -0
  114. data/ext/libcouchbase/src/operations/durability-seqno.cc +3 -0
  115. data/ext/libcouchbase/src/operations/durability.cc +24 -2
  116. data/ext/libcouchbase/src/operations/durability_internal.h +19 -0
  117. data/ext/libcouchbase/src/operations/get.cc +4 -2
  118. data/ext/libcouchbase/src/operations/observe-seqno.cc +1 -0
  119. data/ext/libcouchbase/src/operations/observe.cc +113 -62
  120. data/ext/libcouchbase/src/operations/ping.cc +246 -67
  121. data/ext/libcouchbase/src/operations/remove.cc +2 -1
  122. data/ext/libcouchbase/src/operations/store.cc +17 -14
  123. data/ext/libcouchbase/src/operations/touch.cc +3 -0
  124. data/ext/libcouchbase/src/packetutils.h +68 -4
  125. data/ext/libcouchbase/src/probes.d +132 -161
  126. data/ext/libcouchbase/src/rdb/bigalloc.c +1 -1
  127. data/ext/libcouchbase/src/retryq.cc +6 -2
  128. data/ext/libcouchbase/src/rnd.cc +68 -0
  129. data/ext/libcouchbase/src/rnd.h +39 -0
  130. data/ext/libcouchbase/src/settings.c +27 -0
  131. data/ext/libcouchbase/src/settings.h +67 -3
  132. data/ext/libcouchbase/src/ssl/CMakeLists.txt +0 -12
  133. data/ext/libcouchbase/src/ssl/ssl_common.c +23 -4
  134. data/ext/libcouchbase/src/strcodecs/base64.c +141 -16
  135. data/ext/libcouchbase/src/strcodecs/strcodecs.h +16 -1
  136. data/ext/libcouchbase/src/trace.h +68 -61
  137. data/ext/libcouchbase/src/tracing/span.cc +289 -0
  138. data/ext/libcouchbase/src/tracing/threshold_logging_tracer.cc +171 -0
  139. data/ext/libcouchbase/src/tracing/tracer.cc +53 -0
  140. data/ext/libcouchbase/src/tracing/tracing-internal.h +213 -0
  141. data/ext/libcouchbase/src/utilities.c +5 -0
  142. data/ext/libcouchbase/src/vbucket/CMakeLists.txt +2 -2
  143. data/ext/libcouchbase/src/vbucket/vbucket.c +50 -18
  144. data/ext/libcouchbase/src/views/docreq.cc +26 -1
  145. data/ext/libcouchbase/src/views/docreq.h +17 -0
  146. data/ext/libcouchbase/src/views/viewreq.cc +64 -1
  147. data/ext/libcouchbase/src/views/viewreq.h +21 -0
  148. data/ext/libcouchbase/tests/CMakeLists.txt +6 -6
  149. data/ext/libcouchbase/tests/basic/t_base64.cc +34 -6
  150. data/ext/libcouchbase/tests/basic/t_connstr.cc +14 -0
  151. data/ext/libcouchbase/tests/basic/t_creds.cc +10 -10
  152. data/ext/libcouchbase/tests/basic/t_host.cc +22 -2
  153. data/ext/libcouchbase/tests/basic/t_scram.cc +514 -0
  154. data/ext/libcouchbase/tests/check-all.cc +6 -2
  155. data/ext/libcouchbase/tests/iotests/mock-environment.cc +64 -0
  156. data/ext/libcouchbase/tests/iotests/mock-environment.h +27 -1
  157. data/ext/libcouchbase/tests/iotests/t_confmon.cc +2 -2
  158. data/ext/libcouchbase/tests/iotests/t_forward.cc +8 -0
  159. data/ext/libcouchbase/tests/iotests/t_netfail.cc +124 -0
  160. data/ext/libcouchbase/tests/iotests/t_smoke.cc +1 -1
  161. data/ext/libcouchbase/tests/iotests/t_snappy.cc +316 -0
  162. data/ext/libcouchbase/tests/socktests/socktest.cc +2 -2
  163. data/ext/libcouchbase/tests/socktests/t_basic.cc +6 -6
  164. data/ext/libcouchbase/tests/socktests/t_manager.cc +1 -1
  165. data/ext/libcouchbase/tests/socktests/t_ssl.cc +1 -1
  166. data/ext/libcouchbase/tools/CMakeLists.txt +1 -1
  167. data/ext/libcouchbase/tools/cbc-handlers.h +17 -0
  168. data/ext/libcouchbase/tools/cbc-n1qlback.cc +7 -4
  169. data/ext/libcouchbase/tools/cbc-pillowfight.cc +408 -100
  170. data/ext/libcouchbase/tools/cbc-proxy.cc +134 -3
  171. data/ext/libcouchbase/tools/cbc-subdoc.cc +1 -2
  172. data/ext/libcouchbase/tools/cbc.cc +113 -8
  173. data/ext/libcouchbase/tools/common/histogram.cc +1 -0
  174. data/ext/libcouchbase/tools/common/options.cc +28 -1
  175. data/ext/libcouchbase/tools/common/options.h +5 -0
  176. data/ext/libcouchbase/tools/docgen/docgen.h +36 -10
  177. data/ext/libcouchbase/tools/docgen/loc.h +5 -4
  178. data/ext/libcouchbase/tools/docgen/seqgen.h +28 -0
  179. data/lib/libcouchbase/ext/libcouchbase/enums.rb +10 -0
  180. data/lib/libcouchbase/n1ql.rb +6 -1
  181. data/lib/libcouchbase/version.rb +1 -1
  182. data/spec/connection_spec.rb +6 -6
  183. metadata +38 -5
  184. data/ext/libcouchbase/cmake/Modules/FindCouchbaseSnappy.cmake +0 -11
  185. data/ext/libcouchbase/src/mc/compress.c +0 -90
  186. data/ext/libcouchbase/tools/common/my_inttypes.h +0 -22
@@ -0,0 +1,171 @@
1
+ /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2018 Couchbase, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #include "internal.h"
19
+
20
+ #include <iostream>
21
+ #include <vector>
22
+
23
+ #define LOGARGS(tracer, lvl) tracer->m_settings, "tracer", LCB_LOG_##lvl, __FILE__, __LINE__
24
+
25
+ using namespace lcb::trace;
26
+
27
+ LIBCOUCHBASE_API lcbtrace_TRACER *lcbtrace_new(lcb_t instance, uint64_t flags)
28
+ {
29
+ if (instance == NULL || (flags & LCBTRACE_F_THRESHOLD) == 0) {
30
+ return NULL;
31
+ }
32
+ return (new ThresholdLoggingTracer(instance))->wrap();
33
+ }
34
+
35
+ extern "C" {
36
+ static void tlt_destructor(lcbtrace_TRACER *wrapper)
37
+ {
38
+ if (wrapper == NULL) {
39
+ return;
40
+ }
41
+ if (wrapper->cookie) {
42
+ ThresholdLoggingTracer *tracer = reinterpret_cast< ThresholdLoggingTracer * >(wrapper->cookie);
43
+ delete tracer;
44
+ wrapper->cookie = NULL;
45
+ }
46
+ delete wrapper;
47
+ }
48
+
49
+ static void tlt_report(lcbtrace_TRACER *wrapper, lcbtrace_SPAN *span)
50
+ {
51
+ if (wrapper == NULL || wrapper->cookie == NULL) {
52
+ return;
53
+ }
54
+
55
+ ThresholdLoggingTracer *tracer = reinterpret_cast< ThresholdLoggingTracer * >(wrapper->cookie);
56
+ if (span->tags[LCBTRACE_TAG_SERVICE] == LCBTRACE_TAG_SERVICE_KV) {
57
+ if (lcbtrace_span_is_orphaned(span)) {
58
+ tracer->add_orphan(span);
59
+ } else {
60
+ tracer->check_threshold(span);
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ lcbtrace_TRACER *ThresholdLoggingTracer::wrap()
67
+ {
68
+ if (m_wrapper) {
69
+ return m_wrapper;
70
+ }
71
+ m_wrapper = new lcbtrace_TRACER();
72
+ m_wrapper->version = 0;
73
+ m_wrapper->flags = 0;
74
+ m_wrapper->cookie = this;
75
+ m_wrapper->destructor = tlt_destructor;
76
+ m_wrapper->v.v0.report = tlt_report;
77
+ return m_wrapper;
78
+ }
79
+
80
+ ReportedSpan ThresholdLoggingTracer::convert(lcbtrace_SPAN *span)
81
+ {
82
+ ReportedSpan orphan;
83
+ orphan.duration = span->duration();
84
+ Json::Value entry;
85
+ entry["operation_name"] = span->m_opname;
86
+ entry["operation_id"] = span->tags[LCBTRACE_TAG_OPERATION_ID];
87
+ entry["last_local_id"] = span->tags[LCBTRACE_TAG_LOCAL_ID];
88
+ entry["last_local_address"] = span->tags[LCBTRACE_TAG_LOCAL_ADDRESS];
89
+ entry["last_remote_address"] = span->tags[LCBTRACE_TAG_PEER_ADDRESS];
90
+ entry["server_us"] = span->tags[LCBTRACE_TAG_PEER_LATENCY];
91
+ entry["total_us"] = (Json::UInt64)orphan.duration;
92
+ orphan.payload = Json::FastWriter().write(entry);
93
+ return orphan;
94
+ }
95
+
96
+ void ThresholdLoggingTracer::add_orphan(lcbtrace_SPAN *span)
97
+ {
98
+ m_orphans.push(convert(span));
99
+ }
100
+
101
+ void ThresholdLoggingTracer::check_threshold(lcbtrace_SPAN *span)
102
+ {
103
+ if (span->duration() > m_settings->tracer_threshold[LCBTRACE_THRESHOLD_KV]) {
104
+ m_threshold.push(convert(span));
105
+ }
106
+ }
107
+
108
+ void ThresholdLoggingTracer::flush_queue(FixedQueue< ReportedSpan > &queue, const char *message)
109
+ {
110
+ std::vector< ReportedSpan > &slice = queue.get_sorted();
111
+ Json::Value entries;
112
+ entries["service"] = "kv";
113
+ entries["count"] = (Json::UInt)slice.size();
114
+ Json::Value top;
115
+ for (size_t ii = 0; ii < slice.size(); ii++) {
116
+ Json::Value entry;
117
+ if (Json::Reader().parse(slice[ii].payload, entry)) {
118
+ top.append(entry);
119
+ }
120
+ }
121
+ entries["top"] = top;
122
+ std::string doc = Json::FastWriter().write(entries);
123
+ if (doc.size() > 0 && doc[doc.size() - 1] == '\n') {
124
+ doc[doc.size() - 1] = '\0';
125
+ }
126
+ lcb_log(LOGARGS(this, WARN), "%s: %s", message, doc.c_str());
127
+ queue.clear();
128
+ }
129
+
130
+ void ThresholdLoggingTracer::flush_orphans()
131
+ {
132
+ lcb_U32 tv = m_settings->tracer_orphaned_queue_flush_interval;
133
+ if (tv == 0) {
134
+ m_oflush.cancel();
135
+ } else {
136
+ m_oflush.rearm(tv);
137
+ }
138
+ if (m_orphans.empty()) {
139
+ return;
140
+ }
141
+ flush_queue(m_orphans, "Orphan responses observed");
142
+ }
143
+
144
+ void ThresholdLoggingTracer::flush_threshold()
145
+ {
146
+ lcb_U32 tv = m_settings->tracer_threshold_queue_flush_interval;
147
+ if (tv == 0) {
148
+ m_tflush.cancel();
149
+ } else {
150
+ m_tflush.rearm(tv);
151
+ }
152
+ if (m_threshold.empty()) {
153
+ return;
154
+ }
155
+ flush_queue(m_threshold, "Operations over threshold");
156
+ }
157
+
158
+ ThresholdLoggingTracer::ThresholdLoggingTracer(lcb_t instance)
159
+ : m_wrapper(NULL), m_settings(instance->settings), m_orphans(LCBT_SETTING(instance, tracer_orphaned_queue_size)),
160
+ m_threshold(LCBT_SETTING(instance, tracer_threshold_queue_size)), m_oflush(instance->iotable, this),
161
+ m_tflush(instance->iotable, this)
162
+ {
163
+ lcb_U32 tv = m_settings->tracer_orphaned_queue_flush_interval;
164
+ if (tv > 0) {
165
+ m_oflush.rearm(tv);
166
+ }
167
+ tv = m_settings->tracer_threshold_queue_flush_interval;
168
+ if (tv > 0) {
169
+ m_tflush.rearm(tv);
170
+ }
171
+ }
@@ -0,0 +1,53 @@
1
+ /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2017 Couchbase, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #include "internal.h"
19
+
20
+ using namespace lcb::trace;
21
+
22
+ LIBCOUCHBASE_API void lcbtrace_destroy(lcbtrace_TRACER *tracer)
23
+ {
24
+ if (tracer && tracer->destructor) {
25
+ tracer->destructor(tracer);
26
+ }
27
+ }
28
+
29
+ LIBCOUCHBASE_API
30
+ lcbtrace_SPAN *lcbtrace_span_start(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, lcbtrace_REF *ref)
31
+ {
32
+ lcbtrace_REF_TYPE type = LCBTRACE_REF_NONE;
33
+ lcbtrace_SPAN *other = NULL;
34
+ if (ref) {
35
+ type = ref->type;
36
+ other = ref->span;
37
+ }
38
+ return new Span(tracer, opname, start, type, other);
39
+ }
40
+
41
+ LIBCOUCHBASE_API
42
+ lcbtrace_TRACER *lcb_get_tracer(lcb_t instance)
43
+ {
44
+ return (instance && instance->settings) ? instance->settings->tracer : NULL;
45
+ }
46
+
47
+ LIBCOUCHBASE_API
48
+ void lcb_set_tracer(lcb_t instance, lcbtrace_TRACER *tracer)
49
+ {
50
+ if (instance && instance->settings) {
51
+ instance->settings->tracer = tracer;
52
+ }
53
+ }
@@ -0,0 +1,213 @@
1
+ /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2018 Couchbase, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #ifndef LCB_TRACING_INTERNAL_H
19
+ #define LCB_TRACING_INTERNAL_H
20
+
21
+ #ifdef LCB_TRACING
22
+ #include <libcouchbase/tracing.h>
23
+ #include "rnd.h"
24
+
25
+ #ifdef __cplusplus
26
+
27
+ #include <queue>
28
+
29
+ namespace lcb
30
+ {
31
+ namespace trace
32
+ {
33
+
34
+ class Span
35
+ {
36
+ public:
37
+ Span(lcbtrace_TRACER *tracer, const char *opname, uint64_t start, lcbtrace_REF_TYPE ref, lcbtrace_SPAN *other);
38
+
39
+ void finish(uint64_t finish);
40
+ uint64_t duration()
41
+ {
42
+ return m_finish - m_start;
43
+ }
44
+
45
+ template < typename T > void add_tag(const char *name, T value);
46
+
47
+ lcbtrace_TRACER *m_tracer;
48
+ std::string m_opname;
49
+ uint64_t m_span_id;
50
+ uint64_t m_start;
51
+ uint64_t m_finish;
52
+ bool m_orphaned;
53
+ Json::Value tags;
54
+ Span *m_parent;
55
+ };
56
+
57
+ struct ReportedSpan {
58
+ uint64_t duration;
59
+ std::string payload;
60
+
61
+ bool operator<(const ReportedSpan &rhs) const
62
+ {
63
+ return duration < rhs.duration;
64
+ }
65
+ };
66
+
67
+ template < typename T > class FixedQueue
68
+ {
69
+ public:
70
+ explicit FixedQueue(size_t capacity) : m_capacity(capacity) {}
71
+
72
+ void push(T item)
73
+ {
74
+ if (m_items.size() < m_capacity) {
75
+ m_items.push_back(item);
76
+ std::push_heap(m_items.begin(), m_items.end());
77
+ } else {
78
+ std::sort_heap(m_items.begin(), m_items.end());
79
+ if (m_items.front() < item) {
80
+ m_items[0] = item;
81
+ }
82
+ std::make_heap(m_items.begin(), m_items.end());
83
+ }
84
+ }
85
+
86
+ size_t size()
87
+ {
88
+ return m_items.size();
89
+ }
90
+
91
+ bool empty()
92
+ {
93
+ return m_items.empty();
94
+ }
95
+
96
+ void clear()
97
+ {
98
+ m_items.clear();
99
+ }
100
+
101
+ std::vector< T > &get_sorted()
102
+ {
103
+ std::sort_heap(m_items.begin(), m_items.end());
104
+ return m_items;
105
+ }
106
+
107
+ private:
108
+ size_t m_capacity;
109
+ std::vector< T > m_items;
110
+ };
111
+
112
+ class ThresholdLoggingTracer
113
+ {
114
+ lcbtrace_TRACER *m_wrapper;
115
+ lcb_settings *m_settings;
116
+
117
+ FixedQueue< ReportedSpan > m_orphans;
118
+ FixedQueue< ReportedSpan > m_threshold;
119
+
120
+ void flush_queue(FixedQueue< ReportedSpan > &queue, const char *message);
121
+ ReportedSpan convert(lcbtrace_SPAN *span);
122
+
123
+ public:
124
+ ThresholdLoggingTracer(lcb_t instance);
125
+
126
+ lcbtrace_TRACER *wrap();
127
+ void add_orphan(lcbtrace_SPAN *span);
128
+ void check_threshold(lcbtrace_SPAN *span);
129
+
130
+ void flush_orphans();
131
+ void flush_threshold();
132
+
133
+ lcb::io::Timer< ThresholdLoggingTracer, &ThresholdLoggingTracer::flush_orphans > m_oflush;
134
+ lcb::io::Timer< ThresholdLoggingTracer, &ThresholdLoggingTracer::flush_threshold > m_tflush;
135
+ };
136
+
137
+ } // namespace trace
138
+ } // namespace lcb
139
+
140
+ extern "C" {
141
+ #endif
142
+ LCB_INTERNAL_API
143
+ void lcbtrace_span_add_system_tags(lcbtrace_SPAN *span, lcb_settings *settings, const char *service);
144
+ LCB_INTERNAL_API
145
+ void lcbtrace_span_set_parent(lcbtrace_SPAN *span, lcbtrace_SPAN *parent);
146
+ LCB_INTERNAL_API
147
+ void lcbtrace_span_set_orphaned(lcbtrace_SPAN *span, int val);
148
+
149
+ #define LCBTRACE_KV_START(settings, cmd, operation_name, opaque, outspan) \
150
+ if ((settings)->tracer) { \
151
+ lcbtrace_REF ref; \
152
+ char opid[20] = {}; \
153
+ snprintf(opid, sizeof(opid), "0x%x", (int)opaque); \
154
+ ref.type = LCBTRACE_REF_CHILD_OF; \
155
+ ref.span = (cmd->_hashkey.type & LCB_KV_TRACESPAN) ? (lcbtrace_SPAN *)cmd->_hashkey.contig.bytes : NULL; \
156
+ outspan = lcbtrace_span_start((settings)->tracer, operation_name, LCBTRACE_NOW, &ref); \
157
+ lcbtrace_span_add_tag_str(outspan, LCBTRACE_TAG_OPERATION_ID, opid); \
158
+ lcbtrace_span_add_system_tags(outspan, (settings), LCBTRACE_TAG_SERVICE_KV); \
159
+ }
160
+
161
+ #define LCBTRACE_KV_COMPLETE(pipeline, request, response) \
162
+ do { \
163
+ lcbtrace_SPAN *span = MCREQ_PKT_RDATA(request)->span; \
164
+ if (span) { \
165
+ lcbtrace_span_add_tag_uint64(span, LCBTRACE_TAG_PEER_LATENCY, (response)->duration()); \
166
+ lcb::Server *server = static_cast< lcb::Server * >(pipeline); \
167
+ const lcb_host_t &remote = server->get_host(); \
168
+ std::string hh; \
169
+ if (remote.ipv6) { \
170
+ hh.append("[").append(remote.host).append("]:").append(remote.port); \
171
+ } else { \
172
+ hh.append(remote.host).append(":").append(remote.port); \
173
+ } \
174
+ lcbtrace_span_add_tag_str(span, LCBTRACE_TAG_PEER_ADDRESS, hh.c_str()); \
175
+ lcbio_CTX *ctx = server->connctx; \
176
+ if (ctx) { \
177
+ char local_id[34] = {}; \
178
+ snprintf(local_id, sizeof(local_id), "%016" PRIx64 "/%016" PRIx64, \
179
+ (lcb_U64)server->get_settings()->iid, ctx->sock->id); \
180
+ lcbtrace_span_add_tag_str(span, LCBTRACE_TAG_LOCAL_ID, local_id); \
181
+ lcbtrace_span_add_tag_str(span, LCBTRACE_TAG_LOCAL_ADDRESS, \
182
+ lcbio__inet_ntop(&ctx->sock->info->sa_local).c_str()); \
183
+ } \
184
+ } \
185
+ } while (0);
186
+
187
+ #define LCBTRACE_KV_CLOSE(request) \
188
+ do { \
189
+ lcbtrace_SPAN *span = MCREQ_PKT_RDATA(request)->span; \
190
+ if (span) { \
191
+ lcbtrace_span_finish(span, LCBTRACE_NOW); \
192
+ MCREQ_PKT_RDATA(request)->span = NULL; \
193
+ } \
194
+ } while (0);
195
+
196
+ #define LCBTRACE_KV_FINISH(pipeline, request, response) \
197
+ LCBTRACE_KV_COMPLETE(pipeline, request, response) \
198
+ LCBTRACE_KV_CLOSE(request)
199
+
200
+ #ifdef __cplusplus
201
+ }
202
+ #endif
203
+
204
+ #else
205
+
206
+ #define LCBTRACE_KV_START(settings, cmd, operation_name, opaque, outspan)
207
+ #define LCBTRACE_KV_COMPLETE(pipeline, request, response)
208
+ #define LCBTRACE_KV_CLOSE(request)
209
+ #define LCBTRACE_KV_FINISH(pipeline, request, response)
210
+
211
+ #endif /* LCB_TRACING */
212
+
213
+ #endif /* LCB_TRACING_INTERNAL_H */
@@ -34,6 +34,11 @@ extern lcb_uint64_t lcb_byteswap64(lcb_uint64_t val)
34
34
  return ret;
35
35
  }
36
36
 
37
+ extern lcb_uint16_t lcb_byteswap16(lcb_uint16_t val)
38
+ {
39
+ return ((val & 0xff) << 8) | ((val >> 8) & 0xff);
40
+ }
41
+
37
42
  /**
38
43
  * While the C standard library uses 'putenv' for environment variable
39
44
  * manipulation, POSIX defines setenv (which works sanely) but Windows