-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for ClickHouse native protocol
- Loading branch information
Showing
12 changed files
with
352 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#define CLICKHOUSE_QUERY_ID_SIZE 36 | ||
|
||
#define CLICKHOUSE_QUERY_KIND_INITIAL 1 | ||
#define CLICKHOUSE_QUERY_KIND_SECONDARY 2 | ||
|
||
#define CLICKHOUSE_CLIENT_CODE_QUERY 1 | ||
|
||
#define CLICKHOUSE_SERVER_CODE_DATA 1 | ||
#define CLICKHOUSE_SERVER_CODE_EXCEPTION 2 | ||
#define CLICKHOUSE_SERVER_CODE_END_OF_STREAM 5 | ||
|
||
static __always_inline | ||
int is_clickhouse_query(char *buf, __u64 buf_size) { | ||
__u8 b[CLICKHOUSE_QUERY_ID_SIZE+3]; | ||
if (bpf_probe_read(&b, sizeof(b), (void *)buf) < 0) { | ||
return 0; | ||
} | ||
if (b[0] != CLICKHOUSE_CLIENT_CODE_QUERY) { | ||
return 0; | ||
} | ||
int offset = 0; | ||
if (b[1] == 0) { | ||
offset = 2; | ||
} else if (b[1] == CLICKHOUSE_QUERY_ID_SIZE) { | ||
offset = 2 + CLICKHOUSE_QUERY_ID_SIZE; | ||
} else { | ||
return 0; | ||
} | ||
if (b[offset] != CLICKHOUSE_QUERY_KIND_INITIAL && b[offset] != CLICKHOUSE_QUERY_KIND_SECONDARY) { | ||
return 0; | ||
} | ||
return 1; | ||
} | ||
|
||
static __always_inline | ||
int is_clickhouse_response(char *buf, __u32 *status) { | ||
__u8 code = 0; | ||
if (bpf_probe_read(&code, sizeof(code), (void *)buf) < 0) { | ||
return 0; | ||
} | ||
if (code == CLICKHOUSE_SERVER_CODE_DATA || code == CLICKHOUSE_SERVER_CODE_END_OF_STREAM) { | ||
*status = STATUS_OK; | ||
return 1; | ||
} | ||
if (code == CLICKHOUSE_SERVER_CODE_EXCEPTION) { | ||
*status = STATUS_FAILED; | ||
return 1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package l7 | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/ClickHouse/ch-go/proto" | ||
) | ||
|
||
func ParseClickhouse(payload []byte) string { | ||
r := proto.NewReader(bytes.NewReader(payload)) | ||
var err error | ||
if _, err = r.Byte(); err != nil { | ||
return "" | ||
} | ||
if _, err = r.Str(); err != nil { | ||
return "" | ||
} | ||
version := int(proto.FeatureServerQueryTimeInProgress) | ||
info := proto.ClientInfo{} | ||
if err = info.DecodeAware(r, version); err != nil { | ||
return "" | ||
} | ||
if info.ProtocolVersion > 0 { | ||
version = info.ProtocolVersion | ||
} | ||
var s proto.Setting | ||
|
||
for { | ||
if err = s.Decode(r); err != nil { | ||
return "" | ||
} | ||
if s.Key == "" { | ||
break | ||
} | ||
} | ||
if _, err = r.Str(); err != nil { // inter-server secret | ||
return "" | ||
} | ||
if _, err = r.UVarInt(); err != nil { // stage | ||
return "" | ||
} | ||
if _, err = r.UVarInt(); err != nil { // compression | ||
return "" | ||
} | ||
l, err := r.StrLen() | ||
if err != nil { | ||
return "" | ||
} | ||
query := make([]byte, min(l, 1024)) | ||
n, _ := r.Read(query) | ||
query = bytes.TrimSpace(query[:n]) | ||
if len(query) == 0 { | ||
return "" | ||
} | ||
if n < l { | ||
query = append(query[:len(query)-1], []byte("...<TRUNCATED>")...) | ||
} | ||
return string(query) | ||
} |
Oops, something went wrong.