-
Notifications
You must be signed in to change notification settings - Fork 277
/
task_handler.go
126 lines (113 loc) · 3.71 KB
/
task_handler.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
// Licensed to LinDB under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. LinDB licenses this file to you 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 query
import (
"context"
"time"
"github.com/lindb/lindb/config"
"github.com/lindb/lindb/flow"
"github.com/lindb/lindb/internal/concurrent"
"github.com/lindb/lindb/pkg/logger"
"github.com/lindb/lindb/pkg/timeutil"
protoCommonV1 "github.com/lindb/lindb/proto/gen/v1/common"
"github.com/lindb/lindb/rpc"
)
// TaskHandler represents the task rpc handler
type TaskHandler struct {
cfg config.Query
fct rpc.TaskServerFactory
processor TaskProcessor
timeout time.Duration
taskPool concurrent.Pool
logger *logger.Logger
}
// NewTaskHandler creates the task rpc handler
func NewTaskHandler(
cfg config.Query,
fct rpc.TaskServerFactory,
processor TaskProcessor,
pool concurrent.Pool,
) *TaskHandler {
return &TaskHandler{
cfg: cfg,
timeout: cfg.Timeout.Duration(),
taskPool: pool,
fct: fct,
processor: processor,
logger: logger.GetLogger("Query", "TaskHandler"),
}
}
// Handle handles the task request based on grpc stream
func (q *TaskHandler) Handle(stream protoCommonV1.TaskService_HandleServer) (err error) {
clientLogicNode, err := rpc.GetLogicNodeFromContext(stream.Context())
if err != nil {
return err
}
nodeID := clientLogicNode.Indicator()
epoch := q.fct.Register(nodeID, stream)
q.logger.Info("register task stream",
logger.String("client", nodeID), logger.Int64("epoch", epoch))
// when return, the stream is closed, Deregister the stream
defer func() {
if ok := q.fct.Deregister(epoch, nodeID); ok {
q.logger.Info("unregister task stream successfully",
logger.String("client", nodeID), logger.Int64("epoch", epoch))
}
}()
for {
req, err := stream.Recv()
if err != nil {
q.logger.Error("task server stream error", logger.Error(err))
return err
}
q.process(stream.Context(), stream, req)
}
}
// process dispatches request with timeout
func (q *TaskHandler) process(ctx context.Context, stream protoCommonV1.TaskService_HandleServer, req *protoCommonV1.TaskRequest) {
taskCtx := flow.NewTaskContextWithTimeout(ctx, q.timeout)
q.taskPool.Submit(taskCtx.Ctx,
concurrent.NewTask(func() {
if err := q.processor.Process(taskCtx, stream, req); err != nil {
// if process fail, need send response with err
if sendError := stream.Send(&protoCommonV1.TaskResponse{
RequestID: req.RequestID,
Completed: true,
ErrMsg: err.Error(),
SendTime: timeutil.NowNano(),
}); sendError != nil {
q.logger.Error("failed to send error message to target stream",
logger.String("requestID", req.RequestID),
logger.Error(err),
)
}
}
}, func(err error) {
// if process panic, need send response with err
if sendError := stream.Send(&protoCommonV1.TaskResponse{
RequestID: req.RequestID,
Completed: true,
ErrMsg: err.Error(),
SendTime: timeutil.NowNano(),
}); sendError != nil {
q.logger.Error("failed to send error message to target stream",
logger.String("requestID", req.RequestID),
logger.Error(err),
)
}
}))
}