Debian Codegraph @

exp/lmdbsync/testresize/main.go

raw symbols SCIP
1 /*
2 Command testresize is a utility used by the lmdbsync tests to validate its
3 multiprocessing capabilities. An external command like testresize command is
4 required because a process is not allowed to map the same process twice.
5
6 Testresize writes batches of updates into a databaes, transparently handling
7 any lmdb.MapResized or lmdb.MapFull errors that occur. To ensure that resizing
8 behavior is observed testresize waits for input before updating the environment
9 and writes a line to stdout after the update is committed. If testresize
10 process observes zero of either error it will exit with a non-zero exit code.
11
12 Two testresize processes can communicate to each other using two unix pipes, if
13 the output of each pipe is connected to the input of the other. Writing a
14 single line to one of the pipes will cause updates to ping-pong back and forth
15 between processes.
16 */
17 package main
18
19 import (
20 "bufio"
21 "crypto/rand"
22 "flag"
23 "fmt"
24 "io"
25 "log"
26 "os"
27 "time"
28
29 "golang.org/x/net/context"
30
31 "github.com/bmatsuo/lmdb-go/exp/lmdbsync"
32 "github.com/bmatsuo/lmdb-go/lmdb"
33 )
34
35 func main() {
36 numitems := flag.Int64("n", 5<<10, "the number of items to write")
37 chunksize := flag.Int64("c", 100, "the number of items to write per txn")
38 flag.Parse()
39
40 failed := false
41 defer func() {
42 if failed {
43 os.Exit(1)
44 }
45 }()
46 fail := func(err error) {
47 failed = true
48 log.Print(err)
49 }
50
51 err := WriteRandomItems("db", *numitems, *chunksize)
52 if err != nil {
53 fail(err)
54 } else {
55 log.Printf("success")
56 }
57 }
58
59 // WriteRandomItems writes numitem items with chunksize sized values full of
60 // random data.
61 func WriteRandomItems(path string, numitem, chunksize int64) (err error) {
62 env, err := OpenEnv(path)
63 if err != nil {
64 return err
65 }
66 defer env.Close()
67
68 numResize := 0
69 numResized := 0
70 defer func() {
71 log.Printf("%d resizes", numResize)
72 log.Printf("%d size adoptions", numResized)
73 if err == nil {
74 if numResize == 0 {
75 err = fmt.Errorf("process did not resize the memory map")
76 } else if numResized == 0 {
77 err = fmt.Errorf("process did not adopt a new map size")
78 }
79 }
80 }()
81 mapResizedLogger := func(ctx context.Context, env *lmdbsync.Env, err error) (context.Context, error) {
82 if lmdb.IsMapResized(err) {
83 log.Printf("map resized")
84 numResized++
85 }
86 return ctx, err
87 }
88 mapFullLogger := func(ctx context.Context, env *lmdbsync.Env, err error) (context.Context, error) {
89 if lmdb.IsMapFull(err) {
90 log.Printf("resize required")
91 numResize++
92 }
93 return ctx, err
94 }
95 env.Handlers = env.Handlers.Append(
96 handlerFunc(mapResizedLogger),
97 lmdbsync.MapResizedHandler(2, func(int) time.Duration { return 100 * time.Microsecond }),
98 handlerFunc(mapFullLogger),
99 lmdbsync.MapFullHandler(func(size int64) (int64, bool) {
100 newsize := size + 128<<10 // linear scale is bad -- but useful to test
101 log.Printf("oldsize=%d newsize=%d", size, newsize)
102 return newsize, true
103 }),
104 )
105
106 pid := os.Getpid()
107
108 scanner := bufio.NewScanner(os.Stdin)
109 for i := int64(0); i < numitem; {
110 if !scanner.Scan() {
111 return scanner.Err()
112 }
113
114 start := i
115 chunkmax := i + chunksize
116 if chunkmax > numitem {
117 chunkmax = numitem
118 }
119 v := make([]byte, 512)
120 _, err := io.ReadFull(rand.Reader, v)
121 if err != nil {
122 return err
123 }
124 err = env.Update(func(txn *lmdb.Txn) (err error) {
125 dbi, err := txn.OpenRoot(0)
126 if err != nil {
127 return err
128 }
129
130 for i = start; i < chunkmax; i++ {
131 k := fmt.Sprintf("%06d-%016x", pid, i)
132 err = txn.Put(dbi, []byte(k), v, 0)
133 if err != nil {
134 return err
135 }
136 }
137
138 return nil
139 })
140 if err != nil {
141 return err
142 }
143 fmt.Println("ok")
144 }
145
146 return nil
147 }
148
149 // OpenEnv is a helper for opening an lmdbsync.Env.
150 func OpenEnv(path string) (*lmdbsync.Env, error) {
151 env, err := lmdbsync.NewEnv(nil)
152 if err != nil {
153 return nil, err
154 }
155 err = env.Open(path, 0, 0644)
156 if err != nil {
157 env.Close()
158 return nil, err
159 }
160 return env, nil
161 }
162
163 type handlerFunc func(ctx context.Context, env *lmdbsync.Env, err error) (context.Context, error)
164
165 func (fn handlerFunc) HandleTxnErr(ctx context.Context, env *lmdbsync.Env, err error) (context.Context, error) {
166 return fn(ctx, env, err)
167 }