This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
main.go
146 lines (131 loc) · 4.87 KB
/
main.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
/*
Copyright 2020 Google LLC
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 main
import (
"crypto/rand"
"fmt"
"log"
"math"
"math/big"
"net/http"
"os"
"strconv"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
)
// Compare the ranges in sendResults and receiveResults.
// Create error strings for any entries that are successful in sendResults, but not receiveResults.
// Create warning strings for any entries that are in receiveResults, but not successful in sendResults.
// Append an error string if sendError is not nil.
func compareLogs(sendResults rangeResultArr, receiveResults rangeReceivedArr, sendError error) (errorLogs, warningLogs string) {
successSendRangeCount := 0
if sendError != nil {
errorLogs = sendError.Error()
}
for _, senderI := range sendResults {
if senderI.success {
successSendRangeCount++
found := false
for _, receiverI := range receiveResults {
if receiverI.minID <= senderI.minID && receiverI.maxID >= senderI.maxID {
found = true
if receiverI.minID != senderI.minID || receiverI.maxID != senderI.maxID {
warningLogs = fmt.Sprintf("%sExtra elements in received range for sender range %+v\n", warningLogs, senderI)
}
}
}
if !found {
errorLogs = fmt.Sprintf("%sElements not received from %+v\n", errorLogs, senderI)
}
}
}
if successSendRangeCount == 0 {
errorLogs = fmt.Sprintf("%sNo successful sends\n", errorLogs)
}
if successSendRangeCount < len(receiveResults) {
warningLogs = fmt.Sprintf("%sExtra range in received range set\n", warningLogs)
}
return errorLogs, warningLogs
}
func main() {
sinkURI := os.Getenv("K_SINK")
if len(sinkURI) == 0 {
log.Fatalf("No SINK URI specified\n")
}
delayMSString := os.Getenv("DELAY_MS")
delayMS, err := strconv.Atoi(delayMSString)
if err != nil || delayMS < 0 {
log.Fatalf("Invalid delay millisecond (%s)", delayMSString)
}
postStopWaitSecsString := os.Getenv("POST_STOP_SECS")
postStopWaitSecs, err := strconv.Atoi(postStopWaitSecsString)
if err != nil || postStopWaitSecs < 1 {
log.Fatalf("Invalid post stop wait seconds (%s)", postStopWaitSecsString)
}
fmt.Printf("Opening client to %s\n", sinkURI)
httpOpts := []cehttp.Option{
cloudevents.WithTarget(sinkURI),
}
transport, err := cloudevents.NewHTTP(httpOpts...)
if err != nil {
log.Fatalf("failed to create transport: %v\n", err)
}
ceClient, err := cloudevents.NewClient(transport)
if err != nil {
log.Fatalf("Unable to create ceClient: %s ", err)
}
fmt.Printf("Client created to %s\n", sinkURI)
bint, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
log.Fatalf("Error creating stracker random source id: %v", bint)
}
source := fmt.Sprintf("stracker_%s", bint)
// Start the receive goroutine
receiver := StartReceiver(source, time.Duration(postStopWaitSecs)*time.Second)
sendPacer := makeSender(ceClient, receiver, source, time.Duration(delayMS))
http.HandleFunc("/stop", sendPacer.handleStop)
http.HandleFunc("/start", sendPacer.handleStart)
http.HandleFunc("/ready", sendPacer.handleReady)
http.HandleFunc("/resultsready", sendPacer.handleResultsReady)
http.HandleFunc("/results", sendPacer.handleResults)
go http.ListenAndServe(":8070", nil)
// Run the send path synchronously
sendError := sendPacer.Run()
if sendError != nil && sendPacer.getState() < SendStateFinishing {
fmt.Printf("Sender saw an error and never made it to finishing: %v\n", sendError)
} else {
fmt.Printf("Waiting for receiver to drain\n")
<-receiver.fullyDone
// Get the events the receiver saw
results, dups := receiver.GetResults()
// Compare with the events the sender sent
errorString, warningString := compareLogs(sendPacer.results, results, sendError)
var resultSummary string
// If there were no errors seen, it was a success (warnings don't fail this)
if len(errorString) == 0 {
resultSummary = "success\n"
} else {
resultSummary = "failure\n"
}
sendPacer.resultSummary = fmt.Sprintf("%s%s%sAll send intervals = %s\nAll receive intervals = %s\nDuplicates seen %d\n", resultSummary, errorString, warningString, sendPacer.results, results, dups)
fmt.Printf("%s", sendPacer.resultSummary)
// Allow the result to be retrieved via /results
sendPacer.setState(SendStateDone)
}
// loop forever so that we don't exit and callers can still access the
// /results and /resultsready API's.
for {
time.Sleep(time.Minute)
}
}