forked from Altinity/clickhouse-operator
/
cluster.go
147 lines (129 loc) · 4.63 KB
/
cluster.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"context"
log "github.com/squids-io/clickhouse-operator/pkg/announcer"
chop "github.com/squids-io/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/squids-io/clickhouse-operator/pkg/model/clickhouse"
"github.com/squids-io/clickhouse-operator/pkg/util"
)
// Cluster specifies ClickHouse cluster
type Cluster struct {
*clickhouse.Cluster
}
// NewCluster creates new cluster object
func NewCluster() *Cluster {
return &Cluster{
clickhouse.NewCluster(),
}
}
// SetEndpointCredentials sets endpoint credentials
func (c *Cluster) SetEndpointCredentials(endpointCredentials *clickhouse.ClusterEndpointCredentials) *Cluster {
if c == nil {
return nil
}
c.ClusterEndpointCredentials = endpointCredentials
return c
}
// queryUnzipColumns
func (c *Cluster) queryUnzipColumns(ctx context.Context, hosts []string, sql string, columns ...*[]string) error {
if util.IsContextDone(ctx) {
log.V(2).Info("ctx is done")
return nil
}
if len(hosts) == 0 {
// Nowhere to fetch data from
return nil
}
// Fetch data from any of specified hosts
query, err := c.SetHosts(hosts).QueryAny(ctx, sql)
if err != nil {
return nil
}
if query == nil {
return nil
}
// Some data available, let's fetch it
defer query.Close()
return query.UnzipColumnsAsStrings(columns...)
}
// QueryUnzip2Columns unzips query result into two columns
func (c *Cluster) QueryUnzip2Columns(ctx context.Context, endpoints []string, sql string) ([]string, []string, error) {
var column1 []string
var column2 []string
if err := c.queryUnzipColumns(ctx, endpoints, sql, &column1, &column2); err != nil {
return nil, nil, err
}
return column1, column2, nil
}
// ExecCHI runs set of SQL queries over the whole CHI
func (c *Cluster) ExecCHI(ctx context.Context, chi *chop.ClickHouseInstallation, SQLs []string, _opts ...*clickhouse.QueryOptions) error {
hosts := CreateFQDNs(chi, nil, false)
opts := clickhouse.QueryOptionsNormalize(_opts...)
return c.SetHosts(hosts).ExecAll(ctx, SQLs, opts)
}
// ExecCluster runs set of SQL queries over the cluster
func (c *Cluster) ExecCluster(ctx context.Context, cluster *chop.ChiCluster, SQLs []string, _opts ...*clickhouse.QueryOptions) error {
hosts := CreateFQDNs(cluster, nil, false)
opts := clickhouse.QueryOptionsNormalize(_opts...)
return c.SetHosts(hosts).ExecAll(ctx, SQLs, opts)
}
// ExecShard runs set of SQL queries over the shard replicas
func (c *Cluster) ExecShard(ctx context.Context, shard *chop.ChiShard, SQLs []string, _opts ...*clickhouse.QueryOptions) error {
hosts := CreateFQDNs(shard, nil, false)
opts := clickhouse.QueryOptionsNormalize(_opts...)
return c.SetHosts(hosts).ExecAll(ctx, SQLs, opts)
}
// ExecHost runs set of SQL queries over the replica
func (c *Cluster) ExecHost(ctx context.Context, host *chop.ChiHost, SQLs []string, _opts ...*clickhouse.QueryOptions) error {
hosts := CreateFQDNs(host, chop.ChiHost{}, false)
opts := clickhouse.QueryOptionsNormalize(_opts...)
c.SetHosts(hosts)
if opts.GetSilent() {
c.SetLog(log.Silence())
} else {
c.SetLog(log.New())
}
return c.ExecAll(ctx, SQLs, opts)
}
// QueryHost runs specified query on specified host
func (c *Cluster) QueryHost(ctx context.Context, host *chop.ChiHost, sql string, _opts ...*clickhouse.QueryOptions) (*clickhouse.QueryResult, error) {
hosts := CreateFQDNs(host, chop.ChiHost{}, false)
opts := clickhouse.QueryOptionsNormalize(_opts...)
c.SetHosts(hosts)
if opts.GetSilent() {
c.SetLog(log.Silence())
} else {
c.SetLog(log.New())
}
// Fetch data from any of specified hosts
return c.SetHosts(hosts).QueryAny(ctx, sql)
}
// QueryHostInt runs specified query on specified host and returns one int as a result
func (c *Cluster) QueryHostInt(ctx context.Context, host *chop.ChiHost, sql string, _opts ...*clickhouse.QueryOptions) (int, error) {
if util.IsContextDone(ctx) {
log.V(2).Info("ctx is done")
return 0, nil
}
query, err := c.QueryHost(ctx, host, sql, _opts...)
if query == nil {
return 0, nil
}
defer query.Close()
if err != nil {
return 0, err
}
return query.Int()
}