| 1 |
// Copyright 2022 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"flag" |
| 21 |
"fmt" |
| 22 |
"log" |
| 23 |
"net" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"time" |
| 27 |
|
| 28 |
"cloud.google.com/go/bigtable" |
| 29 |
btpb "cloud.google.com/go/bigtable/apiv2/bigtablepb" |
| 30 |
pb "github.com/googleapis/cloud-bigtable-clients-test/testproxypb" |
| 31 |
"google.golang.org/api/option" |
| 32 |
statpb "google.golang.org/genproto/googleapis/rpc/status" |
| 33 |
"google.golang.org/grpc" |
| 34 |
"google.golang.org/grpc/codes" |
| 35 |
"google.golang.org/grpc/credentials/insecure" |
| 36 |
stat "google.golang.org/grpc/status" |
| 37 |
"google.golang.org/protobuf/types/known/durationpb" |
| 38 |
) |
| 39 |
|
| 40 |
var ( |
| 41 |
port = flag.Int("port", 9999, "The server port") |
| 42 |
logLabel = "cbt-go-proxy" |
| 43 |
) |
| 44 |
|
| 45 |
// rowToProto converts a Bigtable Go client Row struct into a |
| 46 |
// Bigtable protobuf Row struct. It iterates over all of the column families |
| 47 |
// (keys) and ReadItem slices (values) in the client Row struct |
| 48 |
func rowToProto(btRow bigtable.Row) (*btpb.Row, error) { |
| 49 |
pbRow := &btpb.Row{ |
| 50 |
Key: []byte(btRow.Key()), |
| 51 |
} |
| 52 |
|
| 53 |
for fam, ris := range btRow { |
| 54 |
pbFam := &btpb.Family{ |
| 55 |
Name: fam, |
| 56 |
} |
| 57 |
|
| 58 |
for _, col := range ris { |
| 59 |
// Format of column name is `family:columnQualifier` |
| 60 |
colQualifier := strings.Split(col.Column, ":")[1] |
| 61 |
pbCol := &btpb.Column{ |
| 62 |
Qualifier: []byte(colQualifier), |
| 63 |
Cells: []*btpb.Cell{ |
| 64 |
{ |
| 65 |
Value: col.Value, |
| 66 |
TimestampMicros: col.Timestamp.Time().UnixMicro(), |
| 67 |
Labels: col.Labels, |
| 68 |
}, |
| 69 |
}, |
| 70 |
} |
| 71 |
pbFam.Columns = append(pbFam.Columns, pbCol) |
| 72 |
} |
| 73 |
|
| 74 |
pbRow.Families = append(pbRow.Families, pbFam) |
| 75 |
} |
| 76 |
|
| 77 |
return pbRow, nil |
| 78 |
} |
| 79 |
|
| 80 |
// rowSetFromProto translates a Bigtable v2.RowSet object to a Bigtable.RowSet |
| 81 |
// object. |
| 82 |
func rowSetFromProto(rs *btpb.RowSet) bigtable.RowSet { |
| 83 |
rowKeys := rs.GetRowKeys() |
| 84 |
rowRanges := rs.GetRowRanges() |
| 85 |
|
| 86 |
if len(rowKeys) == 0 && len(rowRanges) == 0 { |
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
// Convert all rowKeys into single-row RowRanges |
| 91 |
if len(rowKeys) > 0 { |
| 92 |
var rowList bigtable.RowList |
| 93 |
for _, b := range rowKeys { |
| 94 |
rowList = append(rowList, string(b)) |
| 95 |
} |
| 96 |
return rowList |
| 97 |
} |
| 98 |
|
| 99 |
if len(rowRanges) > 0 { |
| 100 |
rowRangeList := make(bigtable.RowRangeList, 0) |
| 101 |
for _, rrs := range rowRanges { |
| 102 |
var start, end string |
| 103 |
var rr bigtable.RowRange |
| 104 |
|
| 105 |
switch rrs.StartKey.(type) { |
| 106 |
case *btpb.RowRange_StartKeyClosed: |
| 107 |
start = string(rrs.GetStartKeyClosed()) |
| 108 |
case *btpb.RowRange_StartKeyOpen: |
| 109 |
start = string(rrs.GetStartKeyOpen()) |
| 110 |
} |
| 111 |
|
| 112 |
switch rrs.EndKey.(type) { |
| 113 |
case *btpb.RowRange_EndKeyClosed: |
| 114 |
end = string(rrs.GetEndKeyClosed()) |
| 115 |
rr = bigtable.NewRange(start, end) |
| 116 |
case *btpb.RowRange_EndKeyOpen: |
| 117 |
end = string(rrs.GetEndKeyOpen()) |
| 118 |
rr = bigtable.NewRange(start, end) |
| 119 |
default: |
| 120 |
// If not set, get the infinite row range |
| 121 |
rr = bigtable.InfiniteRange(start) |
| 122 |
} |
| 123 |
|
| 124 |
rowRangeList = append(rowRangeList, rr) |
| 125 |
} |
| 126 |
return rowRangeList |
| 127 |
} |
| 128 |
return nil |
| 129 |
} |
| 130 |
|
| 131 |
// mutationFromProto translates a slice of Bigtable v2.Mutation objects into |
| 132 |
// a single Bigtable.Mutation object. |
| 133 |
func mutationFromProto(mPbs []*btpb.Mutation) *bigtable.Mutation { |
| 134 |
m := bigtable.NewMutation() |
| 135 |
for _, mpb := range mPbs { |
| 136 |
|
| 137 |
switch mut := mpb.Mutation.(type) { |
| 138 |
case *btpb.Mutation_DeleteFromColumn_: |
| 139 |
del := mut |
| 140 |
fam := del.DeleteFromColumn.FamilyName |
| 141 |
col := del.DeleteFromColumn.ColumnQualifier |
| 142 |
|
| 143 |
if del.DeleteFromColumn.TimeRange != nil { |
| 144 |
start := bigtable.Time(time.UnixMicro(del.DeleteFromColumn.TimeRange.StartTimestampMicros)) |
| 145 |
end := bigtable.Time(time.UnixMicro(del.DeleteFromColumn.TimeRange.EndTimestampMicros)) |
| 146 |
m.DeleteTimestampRange(fam, string(col), start, end) |
| 147 |
} else { |
| 148 |
m.DeleteCellsInColumn(fam, string(col)) |
| 149 |
} |
| 150 |
|
| 151 |
case *btpb.Mutation_DeleteFromFamily_: |
| 152 |
del := mut |
| 153 |
fam := del.DeleteFromFamily.FamilyName |
| 154 |
m.DeleteCellsInFamily(fam) |
| 155 |
|
| 156 |
case *btpb.Mutation_DeleteFromRow_: |
| 157 |
m.DeleteRow() |
| 158 |
|
| 159 |
case *btpb.Mutation_SetCell_: |
| 160 |
setCell := mut |
| 161 |
fam := setCell.SetCell.FamilyName |
| 162 |
col := setCell.SetCell.ColumnQualifier |
| 163 |
val := setCell.SetCell.Value |
| 164 |
ts := setCell.SetCell.TimestampMicros |
| 165 |
bts := bigtable.Time(time.UnixMicro(ts)) |
| 166 |
m.Set(fam, string(col), bts, val) |
| 167 |
|
| 168 |
} |
| 169 |
} |
| 170 |
return m |
| 171 |
} |
| 172 |
|
| 173 |
// filterFromProto translates a Bigtable v2.RowFilter object into a Bigtable |
| 174 |
// Filter object. |
| 175 |
func filterFromProto(rfPb *btpb.RowFilter) *bigtable.Filter { |
| 176 |
var f *bigtable.Filter |
| 177 |
switch fpb := rfPb.Filter.(type) { |
| 178 |
case *btpb.RowFilter_Chain_: |
| 179 |
c := fpb |
| 180 |
var fs []bigtable.Filter |
| 181 |
for _, cfpb := range c.Chain.Filters { |
| 182 |
cf := filterFromProto(cfpb) |
| 183 |
fs = append(fs, *cf) |
| 184 |
} |
| 185 |
cf := bigtable.ChainFilters(fs...) |
| 186 |
f = &cf |
| 187 |
|
| 188 |
case *btpb.RowFilter_Interleave_: |
| 189 |
i := fpb |
| 190 |
fs := make([]bigtable.Filter, 0) |
| 191 |
for _, ipb := range i.Interleave.Filters { |
| 192 |
ipbf := filterFromProto(ipb) |
| 193 |
fs = append(fs, *ipbf) |
| 194 |
} |
| 195 |
inf := bigtable.InterleaveFilters(fs...) |
| 196 |
f = &inf |
| 197 |
|
| 198 |
case *btpb.RowFilter_Condition_: |
| 199 |
cond := fpb |
| 200 |
|
| 201 |
tf := filterFromProto(cond.Condition.TrueFilter) |
| 202 |
ff := filterFromProto(cond.Condition.TrueFilter) |
| 203 |
pf := filterFromProto(cond.Condition.PredicateFilter) |
| 204 |
|
| 205 |
cf := bigtable.ConditionFilter(*pf, *tf, *ff) |
| 206 |
f = &cf |
| 207 |
|
| 208 |
case *btpb.RowFilter_Sink: |
| 209 |
// Not currently supported. |
| 210 |
f = nil |
| 211 |
|
| 212 |
case *btpb.RowFilter_PassAllFilter: |
| 213 |
p := bigtable.PassAllFilter() |
| 214 |
f = &p |
| 215 |
|
| 216 |
case *btpb.RowFilter_BlockAllFilter: |
| 217 |
b := bigtable.BlockAllFilter() |
| 218 |
f = &b |
| 219 |
|
| 220 |
case *btpb.RowFilter_RowKeyRegexFilter: |
| 221 |
rf := fpb |
| 222 |
re := rf.RowKeyRegexFilter |
| 223 |
rrf := bigtable.RowKeyFilter(string(re)) |
| 224 |
f = &rrf |
| 225 |
|
| 226 |
case *btpb.RowFilter_RowSampleFilter: |
| 227 |
rsf := fpb |
| 228 |
rs := rsf.RowSampleFilter |
| 229 |
rf := bigtable.RowSampleFilter(rs) |
| 230 |
f = &rf |
| 231 |
|
| 232 |
case *btpb.RowFilter_FamilyNameRegexFilter: |
| 233 |
fnf := fpb |
| 234 |
re := fnf.FamilyNameRegexFilter |
| 235 |
fn := bigtable.FamilyFilter(re) |
| 236 |
f = &fn |
| 237 |
|
| 238 |
case *btpb.RowFilter_ColumnQualifierRegexFilter: |
| 239 |
cqf := fpb |
| 240 |
re := cqf.ColumnQualifierRegexFilter |
| 241 |
cq := bigtable.ColumnFilter(string(re)) |
| 242 |
f = &cq |
| 243 |
|
| 244 |
case *btpb.RowFilter_ColumnRangeFilter: |
| 245 |
crf := fpb |
| 246 |
cr := crf.ColumnRangeFilter |
| 247 |
|
| 248 |
var start, end string |
| 249 |
switch sf := cr.StartQualifier; sf.(type) { |
| 250 |
case *btpb.ColumnRange_StartQualifierOpen: |
| 251 |
start = string(sf.(*btpb.ColumnRange_StartQualifierOpen).StartQualifierOpen) |
| 252 |
case *btpb.ColumnRange_StartQualifierClosed: |
| 253 |
start = string(sf.(*btpb.ColumnRange_StartQualifierClosed).StartQualifierClosed) |
| 254 |
} |
| 255 |
|
| 256 |
switch ef := cr.EndQualifier; ef.(type) { |
| 257 |
case *btpb.ColumnRange_EndQualifierClosed: |
| 258 |
end = string(ef.(*btpb.ColumnRange_EndQualifierClosed).EndQualifierClosed) |
| 259 |
case *btpb.ColumnRange_EndQualifierOpen: |
| 260 |
end = string(ef.(*btpb.ColumnRange_EndQualifierOpen).EndQualifierOpen) |
| 261 |
} |
| 262 |
|
| 263 |
cf := bigtable.ColumnRangeFilter(cr.FamilyName, start, end) |
| 264 |
f = &cf |
| 265 |
|
| 266 |
case *btpb.RowFilter_TimestampRangeFilter: |
| 267 |
trf := fpb |
| 268 |
tsr := trf.TimestampRangeFilter |
| 269 |
|
| 270 |
tsf := bigtable.TimestampRangeFilter(time.UnixMicro(tsr.StartTimestampMicros), time.UnixMicro(tsr.EndTimestampMicros)) |
| 271 |
f = &tsf |
| 272 |
|
| 273 |
case *btpb.RowFilter_ValueRegexFilter: |
| 274 |
vrf := fpb |
| 275 |
re := vrf.ValueRegexFilter |
| 276 |
vr := bigtable.ValueFilter(string(re)) |
| 277 |
f = &vr |
| 278 |
|
| 279 |
case *btpb.RowFilter_ValueRangeFilter: |
| 280 |
vrf := fpb |
| 281 |
|
| 282 |
var start, end []byte |
| 283 |
switch sv := vrf.ValueRangeFilter.StartValue; sv.(type) { |
| 284 |
case *btpb.ValueRange_StartValueOpen: |
| 285 |
start = sv.(*btpb.ValueRange_StartValueOpen).StartValueOpen |
| 286 |
case *btpb.ValueRange_StartValueClosed: |
| 287 |
start = sv.(*btpb.ValueRange_StartValueClosed).StartValueClosed |
| 288 |
} |
| 289 |
|
| 290 |
switch ev := vrf.ValueRangeFilter.EndValue; ev.(type) { |
| 291 |
case *btpb.ValueRange_EndValueOpen: |
| 292 |
end = ev.(*btpb.ValueRange_EndValueOpen).EndValueOpen |
| 293 |
case *btpb.ValueRange_EndValueClosed: |
| 294 |
end = ev.(*btpb.ValueRange_EndValueClosed).EndValueClosed |
| 295 |
} |
| 296 |
|
| 297 |
vr := bigtable.ValueRangeFilter(start, end) |
| 298 |
f = &vr |
| 299 |
|
| 300 |
case *btpb.RowFilter_CellsPerRowOffsetFilter: |
| 301 |
cof := fpb |
| 302 |
off := cof.CellsPerRowOffsetFilter |
| 303 |
co := bigtable.CellsPerRowOffsetFilter(int(off)) |
| 304 |
f = &co |
| 305 |
|
| 306 |
case *btpb.RowFilter_CellsPerRowLimitFilter: |
| 307 |
clf := fpb |
| 308 |
lim := clf.CellsPerRowLimitFilter |
| 309 |
cl := bigtable.CellsPerRowLimitFilter(int(lim)) |
| 310 |
f = &cl |
| 311 |
|
| 312 |
case *btpb.RowFilter_CellsPerColumnLimitFilter: |
| 313 |
ccf := fpb |
| 314 |
lim := ccf.CellsPerColumnLimitFilter |
| 315 |
cc := bigtable.LatestNFilter(int(lim)) |
| 316 |
f = &cc |
| 317 |
|
| 318 |
case *btpb.RowFilter_StripValueTransformer: |
| 319 |
sv := bigtable.StripValueFilter() |
| 320 |
f = &sv |
| 321 |
|
| 322 |
case *btpb.RowFilter_ApplyLabelTransformer: |
| 323 |
alf := fpb |
| 324 |
l := alf.ApplyLabelTransformer |
| 325 |
al := bigtable.LabelFilter(l) |
| 326 |
f = &al |
| 327 |
} |
| 328 |
return f |
| 329 |
} |
| 330 |
|
| 331 |
// statusFromError converts an error into a Status code. |
| 332 |
func statusFromError(err error) *statpb.Status { |
| 333 |
log.Printf("error: %v\n", err) |
| 334 |
st := &statpb.Status{ |
| 335 |
Code: int32(codes.Unknown), |
| 336 |
Message: fmt.Sprintf("%v", err), |
| 337 |
} |
| 338 |
if s, ok := stat.FromError(err); ok { |
| 339 |
st = &statpb.Status{ |
| 340 |
Code: int32(s.Code()), |
| 341 |
Message: s.Message(), |
| 342 |
} |
| 343 |
} |
| 344 |
return st |
| 345 |
} |
| 346 |
|
| 347 |
// parseTableID extracts a table ID from a table name. |
| 348 |
// For example, a table ID is in the format projects/<project>/instances/<instance>/tables/<tableID> |
| 349 |
// |
| 350 |
// Note that this function does not check all variants and edge cases. It assumes |
| 351 |
// that the test suite used with the test proxy sends *generally* correct requests. |
| 352 |
func parseTableID(tableName string) (tableID string, _ error) { |
| 353 |
paths := strings.Split(tableName, "/") |
| 354 |
|
| 355 |
if len(paths) < 6 { |
| 356 |
return "", errors.New("table resource name does not have the correct format") |
| 357 |
} |
| 358 |
|
| 359 |
tableID = paths[len(paths)-1] |
| 360 |
var err error |
| 361 |
if tableID == "" { |
| 362 |
err = errors.New("cannot read tableID from table name") |
| 363 |
} |
| 364 |
|
| 365 |
return tableID, err |
| 366 |
} |
| 367 |
|
| 368 |
// testClient contains a bigtable.Client object, cancel functions for the calls |
| 369 |
// made using the client, an appProfileID (optionally), and a |
| 370 |
// perOperationTimeout (optionally). |
| 371 |
type testClient struct { |
| 372 |
c *bigtable.Client // c stores the Bigtable client under test |
| 373 |
appProfileID string // appProfileID is currently unused |
| 374 |
perOperationTimeout *durationpb.Duration // perOperationTimeout sets a custom timeout for methods calls on this client |
| 375 |
} |
| 376 |
|
| 377 |
// timeout adds a timeout setting to a context if perOperationTimeout is set on |
| 378 |
// the testClient object. |
| 379 |
func (tc *testClient) timeout(ctx context.Context) (context.Context, context.CancelFunc) { |
| 380 |
if tc.perOperationTimeout != nil { |
| 381 |
return context.WithTimeout(ctx, tc.perOperationTimeout.AsDuration()) |
| 382 |
} |
| 383 |
return context.WithCancel(ctx) |
| 384 |
} |
| 385 |
|
| 386 |
// getCredentialsOptions provides credentials for a Bigtable client. |
| 387 |
// |
| 388 |
// Note: this proxy uses insecure credentials. This function may need to be |
| 389 |
// expanded to support different credential types. |
| 390 |
func getCredentialsOptions(req *pb.CreateClientRequest) (opts []grpc.DialOption, _ error) { |
| 391 |
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 392 |
return opts, nil |
| 393 |
} |
| 394 |
|
| 395 |
// goTestProxyServer represents an instance of the test proxy server. It keeps |
| 396 |
// a reference to individual clients instances (stored in a testClient object). |
| 397 |
type goTestProxyServer struct { |
| 398 |
pb.UnimplementedCloudBigtableV2TestProxyServer |
| 399 |
clientsLock sync.RWMutex // clientsLock prevents simultaneous mutation of the clientIDs map |
| 400 |
clientIDs map[string]*testClient // clientIDs has all of the bigtable.Client objects under test |
| 401 |
} |
| 402 |
|
| 403 |
// client retrieves a testClient from the clientIDs map. You must lock clientsLock before calling |
| 404 |
// this method. |
| 405 |
func (s *goTestProxyServer) client(clientID string) (*testClient, error) { |
| 406 |
client, ok := s.clientIDs[clientID] |
| 407 |
if !ok { |
| 408 |
return nil, fmt.Errorf("client ID %s does not exist", clientID) |
| 409 |
} |
| 410 |
return client, nil |
| 411 |
} |
| 412 |
|
| 413 |
// CreateClient responds to the CreateClient RPC. This method adds a new client |
| 414 |
// instance to the goTestProxyServer |
| 415 |
func (s *goTestProxyServer) CreateClient(ctx context.Context, req *pb.CreateClientRequest) (*pb.CreateClientResponse, error) { |
| 416 |
if req.ClientId == "" || |
| 417 |
req.DataTarget == "" || |
| 418 |
req.ProjectId == "" || |
| 419 |
req.InstanceId == "" { |
| 420 |
return nil, stat.Error(codes.InvalidArgument, |
| 421 |
fmt.Sprintf("%s must provide ClientId, DataTarget, ProjectId, and InstanceId", logLabel)) |
| 422 |
} |
| 423 |
|
| 424 |
s.clientsLock.Lock() |
| 425 |
defer s.clientsLock.Unlock() |
| 426 |
|
| 427 |
if _, exists := s.clientIDs[req.ClientId]; exists { |
| 428 |
return nil, stat.Error(codes.AlreadyExists, |
| 429 |
fmt.Sprintf("%s: ClientID already exists", logLabel)) |
| 430 |
} |
| 431 |
|
| 432 |
opts, err := getCredentialsOptions(req) |
| 433 |
if err != nil { |
| 434 |
return nil, stat.Error(codes.Unauthenticated, |
| 435 |
fmt.Sprintf("%s: failed to get credentials: %v", logLabel, err)) |
| 436 |
} |
| 437 |
|
| 438 |
conn, err := grpc.Dial(req.DataTarget, opts...) |
| 439 |
if err != nil { |
| 440 |
return nil, stat.Error(codes.Unknown, fmt.Sprintf("%s: failed to create connection: %v", logLabel, err)) |
| 441 |
} |
| 442 |
|
| 443 |
config := bigtable.ClientConfig{ |
| 444 |
AppProfile: req.AppProfileId, |
| 445 |
MetricsProvider: bigtable.NoopMetricsProvider{}, |
| 446 |
} |
| 447 |
c, err := bigtable.NewClientWithConfig(ctx, req.ProjectId, req.InstanceId, config, option.WithGRPCConn(conn)) |
| 448 |
if err != nil { |
| 449 |
return nil, stat.Error(codes.Internal, |
| 450 |
fmt.Sprintf("%s: failed to create client: %v", logLabel, err)) |
| 451 |
} |
| 452 |
|
| 453 |
s.clientIDs[req.ClientId] = &testClient{ |
| 454 |
c: c, |
| 455 |
appProfileID: req.AppProfileId, |
| 456 |
perOperationTimeout: req.PerOperationTimeout, |
| 457 |
} |
| 458 |
|
| 459 |
return &pb.CreateClientResponse{}, nil |
| 460 |
} |
| 461 |
|
| 462 |
// CloseClient responds to the CloseClient RPC. This method closes an existing |
| 463 |
// client, making it inaccessible to new requests. |
| 464 |
func (s *goTestProxyServer) CloseClient(ctx context.Context, req *pb.CloseClientRequest) (*pb.CloseClientResponse, error) { |
| 465 |
clientID := req.ClientId |
| 466 |
s.clientsLock.Lock() |
| 467 |
defer s.clientsLock.Unlock() |
| 468 |
|
| 469 |
btc, err := s.client(clientID) |
| 470 |
if err != nil { |
| 471 |
return nil, err |
| 472 |
} |
| 473 |
btc.c.Close() |
| 474 |
|
| 475 |
return &pb.CloseClientResponse{}, nil |
| 476 |
} |
| 477 |
|
| 478 |
// RemoveClient responds to the RemoveClient RPC. This method removes an |
| 479 |
// existing client from the goTestProxyServer |
| 480 |
func (s *goTestProxyServer) RemoveClient(ctx context.Context, req *pb.RemoveClientRequest) (*pb.RemoveClientResponse, error) { |
| 481 |
clientID := req.ClientId |
| 482 |
|
| 483 |
s.clientsLock.Lock() |
| 484 |
defer s.clientsLock.Unlock() |
| 485 |
|
| 486 |
// RemoveClient can ignore whether the client accepts new requests |
| 487 |
_, err := s.client(clientID) |
| 488 |
if err != nil { |
| 489 |
return nil, stat.Error(codes.InvalidArgument, |
| 490 |
fmt.Sprintf("%s: ClientID does not exist", logLabel)) |
| 491 |
} |
| 492 |
delete(s.clientIDs, clientID) |
| 493 |
|
| 494 |
return &pb.RemoveClientResponse{}, nil |
| 495 |
} |
| 496 |
|
| 497 |
// ReadRow responds to the ReadRow RPC. This method gets all of the column |
| 498 |
// data for a single row in the Table. |
| 499 |
func (s *goTestProxyServer) ReadRow(ctx context.Context, req *pb.ReadRowRequest) (*pb.RowResult, error) { |
| 500 |
s.clientsLock.RLock() |
| 501 |
btc, err := s.client(req.ClientId) |
| 502 |
if err != nil { |
| 503 |
return nil, err |
| 504 |
} |
| 505 |
s.clientsLock.RUnlock() |
| 506 |
|
| 507 |
tid, err := parseTableID(req.TableName) |
| 508 |
if err != nil { |
| 509 |
return nil, err |
| 510 |
} |
| 511 |
t := btc.c.Open(tid) |
| 512 |
|
| 513 |
res := &pb.RowResult{ |
| 514 |
Status: &statpb.Status{ |
| 515 |
Code: int32(codes.OK), |
| 516 |
}, |
| 517 |
Row: &btpb.Row{}, |
| 518 |
} |
| 519 |
|
| 520 |
ctx, cancel := btc.timeout(ctx) |
| 521 |
defer cancel() |
| 522 |
|
| 523 |
r, err := t.ReadRow(ctx, req.RowKey) |
| 524 |
if err != nil { |
| 525 |
res.Status = statusFromError(err) |
| 526 |
return res, nil |
| 527 |
} |
| 528 |
|
| 529 |
if r == nil { |
| 530 |
return res, nil |
| 531 |
} |
| 532 |
|
| 533 |
pbRow, err := rowToProto(r) |
| 534 |
if err != nil { |
| 535 |
return nil, err |
| 536 |
} |
| 537 |
|
| 538 |
res.Row = pbRow |
| 539 |
return res, nil |
| 540 |
} |
| 541 |
|
| 542 |
// ReadRows responds to the ReadRows RPC. This method gets all of the column |
| 543 |
// data for a set of rows, a range of rows, or the entire table. |
| 544 |
func (s *goTestProxyServer) ReadRows(ctx context.Context, req *pb.ReadRowsRequest) (*pb.RowsResult, error) { |
| 545 |
s.clientsLock.RLock() |
| 546 |
btc, err := s.client(req.ClientId) |
| 547 |
s.clientsLock.RUnlock() |
| 548 |
|
| 549 |
if err != nil { |
| 550 |
return nil, err |
| 551 |
} |
| 552 |
|
| 553 |
rrq := req.GetRequest() |
| 554 |
|
| 555 |
if rrq == nil { |
| 556 |
log.Printf("missing inner request: %v\n", rrq) |
| 557 |
return nil, stat.Error(codes.InvalidArgument, "request to ReadRows() is missing inner request") |
| 558 |
|
| 559 |
} |
| 560 |
|
| 561 |
tid, err := parseTableID(rrq.TableName) |
| 562 |
if err != nil { |
| 563 |
return nil, err |
| 564 |
} |
| 565 |
t := btc.c.Open(tid) |
| 566 |
|
| 567 |
rowPbs := rrq.Rows |
| 568 |
rs := rowSetFromProto(rowPbs) |
| 569 |
|
| 570 |
ctx, cancel := btc.timeout(ctx) |
| 571 |
defer cancel() |
| 572 |
|
| 573 |
var c int32 |
| 574 |
var rowsPb []*btpb.Row |
| 575 |
|
| 576 |
lim := req.GetCancelAfterRows() |
| 577 |
|
| 578 |
reversed := req.GetRequest().GetReversed() |
| 579 |
opts := []bigtable.ReadOption{} |
| 580 |
if reversed { |
| 581 |
opts = append(opts, bigtable.ReverseScan()) |
| 582 |
} |
| 583 |
|
| 584 |
rowsLimit := req.GetRequest().GetRowsLimit() |
| 585 |
if rowsLimit > 0 { |
| 586 |
opts = append(opts, bigtable.LimitRows(rowsLimit)) |
| 587 |
} |
| 588 |
err = t.ReadRows(ctx, rs, func(r bigtable.Row) bool { |
| 589 |
|
| 590 |
c++ |
| 591 |
if c == lim { |
| 592 |
return false |
| 593 |
} |
| 594 |
rpb, err := rowToProto(r) |
| 595 |
if err != nil { |
| 596 |
return false |
| 597 |
} |
| 598 |
rowsPb = append(rowsPb, rpb) |
| 599 |
return true |
| 600 |
}, opts...) |
| 601 |
|
| 602 |
res := &pb.RowsResult{ |
| 603 |
Status: &statpb.Status{ |
| 604 |
Code: int32(codes.OK), |
| 605 |
}, |
| 606 |
Rows: []*btpb.Row{}, |
| 607 |
} |
| 608 |
|
| 609 |
if err != nil { |
| 610 |
res.Status = statusFromError(err) |
| 611 |
return res, nil |
| 612 |
} |
| 613 |
|
| 614 |
res.Rows = rowsPb |
| 615 |
|
| 616 |
return res, nil |
| 617 |
} |
| 618 |
|
| 619 |
// MutateRow responds to the MutateRow RPC. This methods applies a series of |
| 620 |
// changes (or deletions) to a single row in a table. |
| 621 |
func (s *goTestProxyServer) MutateRow(ctx context.Context, req *pb.MutateRowRequest) (*pb.MutateRowResult, error) { |
| 622 |
s.clientsLock.RLock() |
| 623 |
btc, err := s.client(req.ClientId) |
| 624 |
s.clientsLock.RUnlock() |
| 625 |
|
| 626 |
if err != nil { |
| 627 |
return nil, err |
| 628 |
} |
| 629 |
|
| 630 |
rrq := req.GetRequest() |
| 631 |
if rrq == nil { |
| 632 |
return nil, stat.Error(codes.InvalidArgument, "request to MutateRow() is missing inner request") |
| 633 |
} |
| 634 |
|
| 635 |
mPbs := rrq.Mutations |
| 636 |
m := mutationFromProto(mPbs) |
| 637 |
|
| 638 |
tid, err := parseTableID(rrq.TableName) |
| 639 |
if err != nil { |
| 640 |
return nil, err |
| 641 |
} |
| 642 |
t := btc.c.Open(tid) |
| 643 |
row := rrq.RowKey |
| 644 |
|
| 645 |
res := &pb.MutateRowResult{ |
| 646 |
Status: &statpb.Status{ |
| 647 |
Code: int32(codes.OK), |
| 648 |
}, |
| 649 |
} |
| 650 |
|
| 651 |
ctx, cancel := btc.timeout(ctx) |
| 652 |
defer cancel() |
| 653 |
|
| 654 |
err = t.Apply(ctx, string(row), m) |
| 655 |
if err != nil { |
| 656 |
res.Status = statusFromError(err) |
| 657 |
return res, nil |
| 658 |
} |
| 659 |
|
| 660 |
return res, nil |
| 661 |
} |
| 662 |
|
| 663 |
// BulkMutateRows responds to the BulkMutateRows RPC. This method applies a |
| 664 |
// series of changes or deletions to multiple rows in a single call. |
| 665 |
func (s *goTestProxyServer) BulkMutateRows(ctx context.Context, req *pb.MutateRowsRequest) (*pb.MutateRowsResult, error) { |
| 666 |
s.clientsLock.RLock() |
| 667 |
btc, err := s.client(req.ClientId) |
| 668 |
s.clientsLock.RUnlock() |
| 669 |
|
| 670 |
if err != nil { |
| 671 |
return nil, err |
| 672 |
} |
| 673 |
|
| 674 |
rrq := req.GetRequest() |
| 675 |
if rrq == nil { |
| 676 |
log.Printf("missing inner request to BulkMutateRows: %v\n", req) |
| 677 |
return nil, stat.Error(codes.InvalidArgument, "request to BulkMutateRows() is missing inner request") |
| 678 |
} |
| 679 |
|
| 680 |
mrs := rrq.Entries |
| 681 |
tid, err := parseTableID(rrq.TableName) |
| 682 |
if err != nil { |
| 683 |
return nil, err |
| 684 |
} |
| 685 |
t := btc.c.Open(tid) |
| 686 |
|
| 687 |
keys := make([]string, len(mrs)) |
| 688 |
muts := make([]*bigtable.Mutation, len(mrs)) |
| 689 |
|
| 690 |
for i, mr := range mrs { |
| 691 |
|
| 692 |
key := string(mr.RowKey) |
| 693 |
m := mutationFromProto(mr.Mutations) |
| 694 |
|
| 695 |
// A little tricky here ... each key corresponds to a single Mutation |
| 696 |
// object, where the indices of each slice must be sync'ed. |
| 697 |
keys[i] = key |
| 698 |
muts[i] = m |
| 699 |
} |
| 700 |
|
| 701 |
res := &pb.MutateRowsResult{ |
| 702 |
Status: &statpb.Status{ |
| 703 |
Code: int32(codes.OK), |
| 704 |
}, |
| 705 |
} |
| 706 |
|
| 707 |
ctx, cancel := btc.timeout(ctx) |
| 708 |
defer cancel() |
| 709 |
|
| 710 |
errs, err := t.ApplyBulk(ctx, keys, muts) |
| 711 |
if err != nil { |
| 712 |
log.Printf("received error from Table.ApplyBulk(): %v", err) |
| 713 |
res.Status = statusFromError(err) |
| 714 |
} |
| 715 |
|
| 716 |
var entries []*btpb.MutateRowsResponse_Entry |
| 717 |
|
| 718 |
if res.Status.Code != int32(codes.OK) { |
| 719 |
// Copy overall error to per mutation error |
| 720 |
for i := range len(muts) { |
| 721 |
me := &btpb.MutateRowsResponse_Entry{ |
| 722 |
Index: int64(i), |
| 723 |
Status: res.Status, |
| 724 |
} |
| 725 |
entries = append(entries, me) |
| 726 |
} |
| 727 |
} else { |
| 728 |
// Iterate over any errors returned, matching indices with errors. If |
| 729 |
// errs is nil, this block is skipped. |
| 730 |
for i, e := range errs { |
| 731 |
var me *btpb.MutateRowsResponse_Entry |
| 732 |
if e != nil { |
| 733 |
st := statusFromError(e) |
| 734 |
me = &btpb.MutateRowsResponse_Entry{ |
| 735 |
Index: int64(i), |
| 736 |
Status: st, |
| 737 |
} |
| 738 |
entries = append(entries, me) |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
res.Entries = entries |
| 743 |
return res, nil |
| 744 |
} |
| 745 |
|
| 746 |
// CheckAndMutateRow responds to the CheckAndMutateRow RPC. This method applies |
| 747 |
// one mutation if a condition is true and another mutation if it is false. |
| 748 |
func (s *goTestProxyServer) CheckAndMutateRow(ctx context.Context, req *pb.CheckAndMutateRowRequest) (*pb.CheckAndMutateRowResult, error) { |
| 749 |
s.clientsLock.RLock() |
| 750 |
btc, err := s.client(req.ClientId) |
| 751 |
s.clientsLock.RUnlock() |
| 752 |
|
| 753 |
if err != nil { |
| 754 |
return nil, err |
| 755 |
} |
| 756 |
|
| 757 |
rrq := req.GetRequest() |
| 758 |
if rrq == nil { |
| 759 |
log.Printf("request to CheckAndMutateRow is missing inner request: received: %v", req) |
| 760 |
return nil, stat.Error(codes.InvalidArgument, "request to CheckAndMutateRow() is missing inner request") |
| 761 |
} |
| 762 |
|
| 763 |
trueMuts := mutationFromProto(rrq.TrueMutations) |
| 764 |
falseMuts := mutationFromProto(rrq.FalseMutations) |
| 765 |
|
| 766 |
rfPb := rrq.PredicateFilter |
| 767 |
var f bigtable.Filter |
| 768 |
|
| 769 |
if rfPb != nil { |
| 770 |
f = *filterFromProto(rfPb) |
| 771 |
} |
| 772 |
|
| 773 |
c := bigtable.NewCondMutation(f, trueMuts, falseMuts) |
| 774 |
|
| 775 |
res := &pb.CheckAndMutateRowResult{ |
| 776 |
Status: &statpb.Status{ |
| 777 |
Code: int32(codes.OK), |
| 778 |
}, |
| 779 |
} |
| 780 |
|
| 781 |
tid, err := parseTableID(rrq.TableName) |
| 782 |
if err != nil { |
| 783 |
return nil, err |
| 784 |
} |
| 785 |
t := btc.c.Open(tid) |
| 786 |
rowKey := string(rrq.RowKey) |
| 787 |
|
| 788 |
var matched bool |
| 789 |
ao := bigtable.GetCondMutationResult(&matched) |
| 790 |
|
| 791 |
ctx, cancel := btc.timeout(ctx) |
| 792 |
defer cancel() |
| 793 |
|
| 794 |
err = t.Apply(ctx, rowKey, c, ao) |
| 795 |
if err != nil { |
| 796 |
log.Printf("received error from Table.Apply: %v", err) |
| 797 |
res.Status = statusFromError(err) |
| 798 |
return res, nil |
| 799 |
} |
| 800 |
|
| 801 |
res.Result = &btpb.CheckAndMutateRowResponse{ |
| 802 |
PredicateMatched: matched, |
| 803 |
} |
| 804 |
|
| 805 |
return res, nil |
| 806 |
} |
| 807 |
|
| 808 |
// SampleRowKeys responds to the SampleRowKeys RPC. This method gets a sampling |
| 809 |
// of the keys available in a table. |
| 810 |
func (s *goTestProxyServer) SampleRowKeys(ctx context.Context, req *pb.SampleRowKeysRequest) (*pb.SampleRowKeysResult, error) { |
| 811 |
s.clientsLock.RLock() |
| 812 |
btc, err := s.client(req.ClientId) |
| 813 |
s.clientsLock.RUnlock() |
| 814 |
|
| 815 |
if err != nil { |
| 816 |
return nil, err |
| 817 |
} |
| 818 |
|
| 819 |
rrq := req.GetRequest() |
| 820 |
if rrq == nil { |
| 821 |
log.Printf("missing inner request to SampleRowKeys: %v\n", req) |
| 822 |
return nil, stat.Error(codes.InvalidArgument, "request to SampleRowKeys() is missing inner request") |
| 823 |
} |
| 824 |
|
| 825 |
res := &pb.SampleRowKeysResult{ |
| 826 |
Status: &statpb.Status{ |
| 827 |
Code: int32(codes.OK), |
| 828 |
}, |
| 829 |
} |
| 830 |
|
| 831 |
ctx, cancel := btc.timeout(ctx) |
| 832 |
defer cancel() |
| 833 |
|
| 834 |
tid, err := parseTableID(rrq.TableName) |
| 835 |
if err != nil { |
| 836 |
return nil, err |
| 837 |
} |
| 838 |
t := btc.c.Open(tid) |
| 839 |
keys, err := t.SampleRowKeys(ctx) |
| 840 |
if err != nil { |
| 841 |
log.Printf("received error from Table.SampleRowKeys(): %v\n", err) |
| 842 |
res.Status = statusFromError(err) |
| 843 |
return res, nil |
| 844 |
} |
| 845 |
|
| 846 |
sk := make([]*btpb.SampleRowKeysResponse, 0) |
| 847 |
for _, k := range keys { |
| 848 |
s := &btpb.SampleRowKeysResponse{ |
| 849 |
RowKey: []byte(k), |
| 850 |
} |
| 851 |
sk = append(sk, s) |
| 852 |
} |
| 853 |
|
| 854 |
res.Samples = sk |
| 855 |
|
| 856 |
return res, nil |
| 857 |
} |
| 858 |
|
| 859 |
// ReadModifyWriteRow responds to the ReadModifyWriteRow RPC. This method |
| 860 |
// applies a non-idempotent change to a row. |
| 861 |
func (s *goTestProxyServer) ReadModifyWriteRow(ctx context.Context, req *pb.ReadModifyWriteRowRequest) (*pb.RowResult, error) { |
| 862 |
s.clientsLock.RLock() |
| 863 |
btc, err := s.client(req.ClientId) |
| 864 |
s.clientsLock.RUnlock() |
| 865 |
|
| 866 |
if err != nil { |
| 867 |
return nil, err |
| 868 |
} |
| 869 |
|
| 870 |
rrq := req.GetRequest() |
| 871 |
if rrq == nil { |
| 872 |
log.Printf("missing inner request to ReadModifyWriteRow: %v\n", req) |
| 873 |
return nil, stat.Error(codes.InvalidArgument, "request to CheckAndMutateRow() is missing inner request") |
| 874 |
} |
| 875 |
|
| 876 |
rpb := rrq.Rules |
| 877 |
rmw := bigtable.NewReadModifyWrite() |
| 878 |
|
| 879 |
for _, rp := range rpb { |
| 880 |
switch r := rp.Rule.(type) { |
| 881 |
case *btpb.ReadModifyWriteRule_AppendValue: |
| 882 |
av := r |
| 883 |
rmw.AppendValue(rp.FamilyName, string(rp.ColumnQualifier), av.AppendValue) |
| 884 |
case *btpb.ReadModifyWriteRule_IncrementAmount: |
| 885 |
ia := r |
| 886 |
rmw.Increment(rp.FamilyName, string(rp.ColumnQualifier), ia.IncrementAmount) |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
res := &pb.RowResult{ |
| 891 |
Status: &statpb.Status{ |
| 892 |
Code: int32(codes.OK), |
| 893 |
}, |
| 894 |
} |
| 895 |
|
| 896 |
tid, err := parseTableID(rrq.TableName) |
| 897 |
if err != nil { |
| 898 |
return nil, err |
| 899 |
} |
| 900 |
t := btc.c.Open(tid) |
| 901 |
k := string(rrq.RowKey) |
| 902 |
|
| 903 |
ctx, cancel := btc.timeout(ctx) |
| 904 |
defer cancel() |
| 905 |
|
| 906 |
r, err := t.ApplyReadModifyWrite(ctx, k, rmw) |
| 907 |
if err != nil { |
| 908 |
res.Status = statusFromError(err) |
| 909 |
return res, nil |
| 910 |
} |
| 911 |
|
| 912 |
rp, err := rowToProto(r) |
| 913 |
if err != nil { |
| 914 |
res.Status = statusFromError(err) |
| 915 |
return res, nil |
| 916 |
} |
| 917 |
|
| 918 |
res.Row = rp |
| 919 |
return res, nil |
| 920 |
} |
| 921 |
|
| 922 |
func (s *goTestProxyServer) mustEmbedUnimplementedCloudBigtableV2TestProxyServer() {} |
| 923 |
|
| 924 |
func newProxyServer(lis net.Listener) *grpc.Server { |
| 925 |
s := grpc.NewServer() |
| 926 |
|
| 927 |
tps := &goTestProxyServer{ |
| 928 |
clientIDs: make(map[string]*testClient), |
| 929 |
} |
| 930 |
|
| 931 |
pb.RegisterCloudBigtableV2TestProxyServer(s, tps) |
| 932 |
log.Printf("server listening at %v", lis.Addr()) |
| 933 |
return s |
| 934 |
} |
| 935 |
|
| 936 |
func main() { |
| 937 |
flag.Parse() |
| 938 |
|
| 939 |
log.Printf("attempting to listen on port %d", *port) |
| 940 |
|
| 941 |
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) |
| 942 |
if err != nil { |
| 943 |
log.Fatalf("failed to listen: %v", err) |
| 944 |
} |
| 945 |
defer lis.Close() |
| 946 |
|
| 947 |
s := newProxyServer(lis) |
| 948 |
if err := s.Serve(lis); err != nil { |
| 949 |
log.Fatalf("failed to serve: %v", err) |
| 950 |
} |
| 951 |
defer s.Stop() |
| 952 |
} |