github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/apply/doc_test.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package apply_test 12 13 import ( 14 "context" 15 "fmt" 16 17 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/apply" 18 ) 19 20 func ExampleTask() { 21 defer setLogging(true)() 22 ctx := context.Background() 23 ents := makeEntries(7) 24 25 sm := getTestStateMachine() 26 dec := newTestDecoder() 27 dec.nonTrivial[5] = true 28 dec.nonLocal[2] = true 29 dec.nonLocal[6] = true 30 dec.shouldReject[3] = true 31 dec.shouldReject[6] = true 32 fmt.Print(` 33 Setting up a batch of seven log entries: 34 - index 2 and 6 are non-local 35 - index 3 and 6 will be rejected 36 - index 5 is not trivial 37 `) 38 39 t := apply.MakeTask(sm, dec) 40 defer t.Close() 41 42 fmt.Println("\nDecode (note that index 2 and 6 are not local):") 43 if err := t.Decode(ctx, ents); err != nil { 44 panic(err) 45 } 46 47 fmt.Println("\nAckCommittedEntriesBeforeApplication:") 48 if err := t.AckCommittedEntriesBeforeApplication(ctx, 10 /* maxIndex */); err != nil { 49 panic(err) 50 } 51 fmt.Print(` 52 Above, only index 1 and 4 get acked early. The command at 5 is 53 non-trivial, so the first batch contains only 1, 2, 3, and 4. An entry 54 must be in the first batch to qualify for acking early. 2 is not local 55 (so there's nobody to ack), and 3 is rejected. We can't ack rejected 56 commands early because the state machine is free to handle them any way 57 it likes. 58 `) 59 60 fmt.Println("\nApplyCommittedEntries:") 61 if err := t.ApplyCommittedEntries(ctx); err != nil { 62 panic(err) 63 } 64 // Output: 65 // 66 // Setting up a batch of seven log entries: 67 // - index 2 and 6 are non-local 68 // - index 3 and 6 will be rejected 69 // - index 5 is not trivial 70 // 71 // Decode (note that index 2 and 6 are not local): 72 // decoding command 1; local=true 73 // decoding command 2; local=false 74 // decoding command 3; local=true 75 // decoding command 4; local=true 76 // decoding command 5; local=true 77 // decoding command 6; local=false 78 // decoding command 7; local=true 79 // 80 // AckCommittedEntriesBeforeApplication: 81 // acknowledging command 1 before application 82 // acknowledging command 4 before application 83 // 84 // Above, only index 1 and 4 get acked early. The command at 5 is 85 // non-trivial, so the first batch contains only 1, 2, 3, and 4. An entry 86 // must be in the first batch to qualify for acking early. 2 is not local 87 // (so there's nobody to ack), and 3 is rejected. We can't ack rejected 88 // commands early because the state machine is free to handle them any way 89 // it likes. 90 // 91 // ApplyCommittedEntries: 92 // applying batch with commands=[1 2 3 4] 93 // applying side-effects of command 1 94 // applying side-effects of command 2 95 // applying side-effects of command 3 96 // applying side-effects of command 4 97 // finishing command 1; rejected=false 98 // finishing and acknowledging command 2; rejected=false 99 // finishing and acknowledging command 3; rejected=true 100 // finishing command 4; rejected=false 101 // applying batch with commands=[5] 102 // applying side-effects of command 5 103 // finishing and acknowledging command 5; rejected=false 104 // applying batch with commands=[6 7] 105 // applying side-effects of command 6 106 // applying side-effects of command 7 107 // finishing and acknowledging command 6; rejected=true 108 // finishing and acknowledging command 7; rejected=false 109 }