|
15 | 15 | package actions
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + |
18 | 22 | "cloud.google.com/go/spanner"
|
19 | 23 | "cloud.google.com/go/spanner/apiv1/spannerpb"
|
20 | 24 | "cloud.google.com/go/spanner/test/cloudexecutor/executor/internal/outputstream"
|
21 | 25 | "cloud.google.com/go/spanner/test/cloudexecutor/executor/internal/utility"
|
| 26 | + executorpb "cloud.google.com/go/spanner/test/cloudexecutor/proto" |
| 27 | + "google.golang.org/api/iterator" |
| 28 | + "google.golang.org/grpc/codes" |
| 29 | + "google.golang.org/grpc/status" |
22 | 30 | )
|
23 | 31 |
|
| 32 | +// ReadActionHandler holds the necessary components required for executorpb.ReadAction. |
| 33 | +type ReadActionHandler struct { |
| 34 | + Action *executorpb.ReadAction |
| 35 | + FlowContext *ExecutionFlowContext |
| 36 | + OutcomeSender *outputstream.OutcomeSender |
| 37 | +} |
| 38 | + |
| 39 | +// ExecuteAction executes a read action request, store the results in the OutcomeSender. |
| 40 | +func (h *ReadActionHandler) ExecuteAction(ctx context.Context) error { |
| 41 | + h.FlowContext.mu.Lock() |
| 42 | + defer h.FlowContext.mu.Unlock() |
| 43 | + log.Printf("Executing read %s:\n %v", h.FlowContext.transactionSeed, h.Action) |
| 44 | + action := h.Action |
| 45 | + var err error |
| 46 | + |
| 47 | + var typeList []*spannerpb.Type |
| 48 | + if action.Index != nil { |
| 49 | + typeList, err = extractTypes(action.GetTable(), action.GetColumn(), h.FlowContext.tableMetadata) |
| 50 | + if err != nil { |
| 51 | + return h.OutcomeSender.FinishWithError(status.Error(codes.InvalidArgument, fmt.Sprintf("Can't extract types from metadata: %s", err))) |
| 52 | + } |
| 53 | + } else { |
| 54 | + typeList, err = h.FlowContext.tableMetadata.GetKeyColumnTypes(action.GetTable()) |
| 55 | + if err != nil { |
| 56 | + return h.OutcomeSender.FinishWithError(status.Error(codes.InvalidArgument, fmt.Sprintf("Can't extract types from metadata: %s", err))) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + keySet, err := utility.KeySetProtoToCloudKeySet(action.GetKeys(), typeList) |
| 61 | + if err != nil { |
| 62 | + return h.OutcomeSender.FinishWithError(status.Error(codes.InvalidArgument, fmt.Sprintf("Can't convert rowSet: %s", err))) |
| 63 | + } |
| 64 | + |
| 65 | + var iter *spanner.RowIterator |
| 66 | + if h.FlowContext.currentActiveTransaction == None { |
| 67 | + return h.OutcomeSender.FinishWithError(status.Error(codes.InvalidArgument, "no active transaction")) |
| 68 | + } else if h.FlowContext.currentActiveTransaction == Batch { |
| 69 | + return h.OutcomeSender.FinishWithError(status.Error(codes.InvalidArgument, "can't execute regular read in a batch transaction")) |
| 70 | + } else if h.FlowContext.currentActiveTransaction == Read { |
| 71 | + txn, err := h.FlowContext.getTransactionForRead() |
| 72 | + if err != nil { |
| 73 | + return h.OutcomeSender.FinishWithError(err) |
| 74 | + } |
| 75 | + h.OutcomeSender.InitForRead(action.GetTable(), action.Index) |
| 76 | + h.FlowContext.numPendingReads++ |
| 77 | + if action.Index != nil { |
| 78 | + iter = txn.ReadUsingIndex(ctx, action.GetTable(), action.GetIndex(), keySet, action.GetColumn()) |
| 79 | + } else { |
| 80 | + iter = txn.Read(ctx, action.GetTable(), keySet, action.GetColumn()) |
| 81 | + } |
| 82 | + } else if h.FlowContext.currentActiveTransaction == ReadWrite { |
| 83 | + txn, err := h.FlowContext.getTransactionForWrite() |
| 84 | + if err != nil { |
| 85 | + return h.OutcomeSender.FinishWithError(err) |
| 86 | + } |
| 87 | + h.OutcomeSender.InitForRead(action.GetTable(), action.Index) |
| 88 | + h.FlowContext.numPendingReads++ |
| 89 | + if action.Index != nil { |
| 90 | + iter = txn.ReadUsingIndex(ctx, action.GetTable(), action.GetIndex(), keySet, action.GetColumn()) |
| 91 | + } else { |
| 92 | + iter = txn.Read(ctx, action.GetTable(), keySet, action.GetColumn()) |
| 93 | + } |
| 94 | + } |
| 95 | + defer iter.Stop() |
| 96 | + log.Printf("Parsing read result %s\n", h.FlowContext.transactionSeed) |
| 97 | + err = processResults(iter, int64(action.GetLimit()), h.OutcomeSender, h.FlowContext) |
| 98 | + if err != nil { |
| 99 | + h.FlowContext.finishRead(status.Code(err)) |
| 100 | + if status.Code(err) == codes.Aborted { |
| 101 | + return h.OutcomeSender.FinishWithTransactionRestarted() |
| 102 | + } |
| 103 | + return h.OutcomeSender.FinishWithError(err) |
| 104 | + } |
| 105 | + h.FlowContext.finishRead(codes.OK) |
| 106 | + return h.OutcomeSender.FinishSuccessfully() |
| 107 | +} |
| 108 | + |
| 109 | +// QueryActionHandler holds the necessary components required for executorpb.QueryAction. |
| 110 | +type QueryActionHandler struct { |
| 111 | + Action *executorpb.QueryAction |
| 112 | + FlowContext *ExecutionFlowContext |
| 113 | + OutcomeSender *outputstream.OutcomeSender |
| 114 | +} |
| 115 | + |
| 116 | +// ExecuteAction executes a query action request, store the results in the OutcomeSender. |
| 117 | +func (h *QueryActionHandler) ExecuteAction(ctx context.Context) error { |
| 118 | + h.FlowContext.mu.Lock() |
| 119 | + defer h.FlowContext.mu.Unlock() |
| 120 | + log.Printf("Executing query %s\n %v\n", h.FlowContext.transactionSeed, h.Action) |
| 121 | + stmt, err := utility.BuildQuery(h.Action) |
| 122 | + if err != nil { |
| 123 | + return h.OutcomeSender.FinishWithError(err) |
| 124 | + } |
| 125 | + |
| 126 | + var iter *spanner.RowIterator |
| 127 | + if h.FlowContext.currentActiveTransaction == None { |
| 128 | + return h.OutcomeSender.FinishWithError(status.Error(codes.InvalidArgument, "no active transaction")) |
| 129 | + } else if h.FlowContext.currentActiveTransaction == Batch { |
| 130 | + return h.OutcomeSender.FinishWithError(status.Error(codes.InvalidArgument, "can't execute regular read in a batch transaction")) |
| 131 | + } else if h.FlowContext.currentActiveTransaction == Read { |
| 132 | + txn, err := h.FlowContext.getTransactionForRead() |
| 133 | + if err != nil { |
| 134 | + return h.OutcomeSender.FinishWithError(err) |
| 135 | + } |
| 136 | + h.OutcomeSender.InitForQuery() |
| 137 | + h.FlowContext.numPendingReads++ |
| 138 | + iter = txn.Query(ctx, stmt) |
| 139 | + } else if h.FlowContext.currentActiveTransaction == ReadWrite { |
| 140 | + txn, err := h.FlowContext.getTransactionForWrite() |
| 141 | + if err != nil { |
| 142 | + return h.OutcomeSender.FinishWithError(err) |
| 143 | + } |
| 144 | + h.OutcomeSender.InitForQuery() |
| 145 | + h.FlowContext.numPendingReads++ |
| 146 | + iter = txn.Query(ctx, stmt) |
| 147 | + } |
| 148 | + defer iter.Stop() |
| 149 | + log.Printf("Parsing query result %s\n", h.FlowContext.transactionSeed) |
| 150 | + err = processResults(iter, 0, h.OutcomeSender, h.FlowContext) |
| 151 | + if err != nil { |
| 152 | + h.FlowContext.finishRead(status.Code(err)) |
| 153 | + if status.Code(err) == codes.Aborted { |
| 154 | + return h.OutcomeSender.FinishWithTransactionRestarted() |
| 155 | + } |
| 156 | + return h.OutcomeSender.FinishWithError(err) |
| 157 | + } |
| 158 | + h.FlowContext.finishRead(codes.OK) |
| 159 | + return h.OutcomeSender.FinishSuccessfully() |
| 160 | +} |
| 161 | + |
| 162 | +// processResults processes a ResultSet from a read/query/dml and store the results in the OutcomeSender. |
24 | 163 | func processResults(iter *spanner.RowIterator, limit int64, outcomeSender *outputstream.OutcomeSender, flowContext *ExecutionFlowContext) error {
|
| 164 | + var rowCount int64 = 0 |
| 165 | + log.Printf("Iterating result set: %s\n", flowContext.transactionSeed) |
| 166 | + for { |
| 167 | + row, err := iter.Next() |
| 168 | + if err == iterator.Done { |
| 169 | + return nil |
| 170 | + } |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + spannerRow, rowType, err := utility.ConvertSpannerRow(row) |
| 175 | + if err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + outcomeSender.SetRowType(rowType) |
| 179 | + // outcomeSender.rowType = rowType |
| 180 | + err = outcomeSender.AppendRow(spannerRow) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + rowCount++ |
| 185 | + if limit > 0 && rowCount >= limit { |
| 186 | + log.Printf("Stopping at row limit: %d", limit) |
| 187 | + break |
| 188 | + } |
| 189 | + } |
| 190 | + log.Printf("Successfully processed result: %s\n", flowContext.transactionSeed) |
25 | 191 | return nil
|
26 | 192 | }
|
27 | 193 |
|
|
0 commit comments