/
router.go
113 lines (92 loc) · 2.72 KB
/
router.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
package routerqpu
import (
"time"
"github.com/dvasilas/proteus/internal/libqpu"
"github.com/dvasilas/proteus/internal/proto/qpuextapi"
"github.com/opentracing/opentracing-go"
"github.com/dvasilas/proteus/internal/proto/qpuapi"
)
// RouterQPU ...
type RouterQPU struct {
adjacentQPUs []*libqpu.AdjacentQPU
conf *libqpu.QPUConfig
}
// ---------------- API Functions -------------------
// InitClass ...
func InitClass(qpu *libqpu.QPU, catchUpDoneCh chan int) (*RouterQPU, error) {
rqpu := &RouterQPU{
adjacentQPUs: qpu.AdjacentQPUs,
conf: qpu.Config,
}
go func() {
time.Sleep(2)
catchUpDoneCh <- 0
}()
return rqpu, nil
}
// ProcessQuerySnapshot ...
func (q *RouterQPU) ProcessQuerySnapshot(query libqpu.ASTQuery, md map[string]string, sync bool, parentSpan opentracing.Span) (<-chan libqpu.LogOperation, <-chan error) {
return nil, nil
}
// ClientQuery ...
func (q *RouterQPU) ClientQuery(query libqpu.ASTQuery, queryStr string, parentSpan opentracing.Span) (*qpuextapi.QueryResp, error) {
queryRespCh := make(chan qpuextapi.QueryResp)
errorCh := make(chan error)
respRecords := make([]*qpuextapi.QueryRespRecord, 0)
subQueryCount := len(q.adjacentQPUs)
for _, adjQPU := range q.adjacentQPUs {
go func(to *libqpu.AdjacentQPU) {
resp, err := to.APIClient.QueryUnary(queryStr)
if err != nil {
errorCh <- err
return
}
queryRespCh <- *resp
}(adjQPU)
}
returnedCount := 0
for {
select {
case resp := <-queryRespCh:
respRecords = append(respRecords, resp.GetRespRecord()...)
returnedCount++
if returnedCount == subQueryCount {
close(queryRespCh)
close(errorCh)
return &qpuextapi.QueryResp{
RespRecord: respRecords,
}, nil
}
case err := <-errorCh:
return nil, err
}
}
}
// QuerySubscribe ...
func (q *RouterQPU) QuerySubscribe(query libqpu.ASTQuery, res *qpuextapi.QueryReq) (chan libqpu.LogOperation, chan bool, chan error) {
return nil, nil, nil
}
// ProcessQuerySubscribe ...
func (q *RouterQPU) ProcessQuerySubscribe(query libqpu.ASTQuery, md map[string]string, sync bool) (int, <-chan libqpu.LogOperation, <-chan error) {
return -1, nil, nil
}
// RemovePersistentQuery ...
func (q *RouterQPU) RemovePersistentQuery(table string, queryID int) {
}
// ---------------- Internal Functions --------------
func (q *RouterQPU) processRespRecord(respRecord libqpu.ResponseRecord, data interface{}, recordCh chan libqpu.ResponseRecord, queryID int) error {
respRecordType, err := respRecord.GetType()
if err != nil {
return err
}
if respRecordType == libqpu.EndOfStream {
close(recordCh)
} else {
recordCh <- respRecord
}
return nil
}
// GetConfig ...
func (q RouterQPU) GetConfig() *qpuapi.ConfigResponse {
return &qpuapi.ConfigResponse{}
}