File: conn.go

package info (click to toggle)
golang-doozer 0.0~git20130909-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 283
  • sloc: makefile: 11
file content (536 lines) | stat: -rw-r--r-- 10,691 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package doozer

import (
	"code.google.com/p/goprotobuf/proto"
	"encoding/binary"
	"errors"
	"github.com/kr/pretty"
	"time"

	"io"
	"log"
	"math/rand"
	"net"
	"net/url"
	"strings"
)

var (
	uriPrefix = "doozer:?"
)

var (
	ErrInvalidUri = errors.New("invalid uri")
)

type txn struct {
	req  request
	resp *response
	err  error
	done chan bool
}

type Conn struct {
	addr    string
	conn    net.Conn
	send    chan *txn
	msg     chan []byte
	err     error
	stop    chan bool
	stopped chan bool
}

func init() {
	rand.Seed(time.Now().UnixNano())
}

// Dial connects to a single doozer server.
func Dial(addr string) (*Conn, error) {
	return dial(addr, -1)
}

// DialTimeout acts like Dial but takes a timeout.
func DialTimeout(addr string, timeout time.Duration) (*Conn, error) {
	return dial(addr, timeout)
}

func dial(addr string, timeout time.Duration) (*Conn, error) {
	var c Conn
	var err error
	c.addr = addr
	if timeout > 0 {
		c.conn, err = net.DialTimeout("tcp", addr, timeout)
	} else {
		c.conn, err = net.Dial("tcp", addr)
	}
	if err != nil {
		return nil, err
	}

	c.send = make(chan *txn)
	c.msg = make(chan []byte)
	c.stop = make(chan bool, 1)
	c.stopped = make(chan bool)
	errch := make(chan error, 1)
	go c.mux(errch)
	go c.readAll(errch)
	return &c, nil
}

// DialUri connects to one of the doozer servers given in `uri`. If `uri`
// contains a cluster name, it will lookup addrs to try in `buri`.  If `uri`
// contains a  secret key, then DialUri will call `Access` with the secret.
func DialUri(uri, buri string) (*Conn, error) {
	return dialUri(uri, buri, -1)
}

// DialUriTimeout acts like DialUri but takes a timeout.
func DialUriTimeout(uri, buri string, timeout time.Duration) (*Conn, error) {
	return dialUri(uri, buri, -1)
}

func dialUri(uri, buri string, timeout time.Duration) (*Conn, error) {
	if !strings.HasPrefix(uri, uriPrefix) {
		return nil, ErrInvalidUri
	}

	q := uri[len(uriPrefix):]
	p, err := url.ParseQuery(q)
	if err != nil {
		return nil, err
	}

	addrs := make([]string, 0)

	name, ok := p["cn"]
	if ok && buri != "" {
		c, err := DialUri(buri, "")
		if err != nil {
			return nil, err
		}

		addrs, err = lookup(c, name[0])
		if err != nil {
			return nil, err
		}
	} else {
		var ok bool
		addrs, ok = p["ca"]
		if !ok {
			return nil, ErrInvalidUri
		}
	}

	c, err := dial(addrs[rand.Int()%len(addrs)], timeout)
	if err != nil {
		return nil, err
	}

	secret, ok := p["sk"]
	if ok {
		err = c.Access(secret[0])
		if err != nil {
			c.Close()
			return nil, err
		}
	}

	return c, nil
}

// Find possible addresses for cluster named name.
func lookup(b *Conn, name string) (as []string, err error) {
	rev, err := b.Rev()
	if err != nil {
		return nil, err
	}

	path := "/ctl/ns/" + name
	names, err := b.Getdir(path, rev, 0, -1)
	if err, ok := err.(*Error); ok && err.Err == ErrNoEnt {
		return nil, nil
	} else if err != nil {
		return nil, err
	}

	path += "/"
	for _, name := range names {
		body, _, err := b.Get(path+name, &rev)
		if err != nil {
			return nil, err
		}
		as = append(as, string(body))
	}
	return as, nil
}

func (c *Conn) call(t *txn) error {
	t.done = make(chan bool)
	select {
	case <-c.stopped:
		return c.err
	case c.send <- t:
		select {
		case <-c.stopped:
			return c.err
		case <-t.done:
			if t.err != nil {
				return t.err
			}
			if t.resp.ErrCode != nil {
				return newError(t)
			}
		}
	}
	return nil
}

// After Close is called, operations on c will return ErrClosed.
func (c *Conn) Close() {
	select {
	case c.stop <- true:
	default:
	}
}

func (c *Conn) mux(errch chan error) {
	txns := make(map[int32]*txn)
	var n int32 // next tag
	var err error

	for {
		select {
		case t := <-c.send:
			// find an unused tag
			for t := txns[n]; t != nil; t = txns[n] {
				n++
			}
			txns[n] = t

			// don't take n's address; it will change
			tag := n
			t.req.Tag = &tag

			var buf []byte
			buf, err = proto.Marshal(&t.req)
			if err != nil {
				txns[n] = nil
				t.err = err
				t.done <- true
				continue
			}

			err = c.write(buf)
			if err != nil {
				goto error
			}
		case buf := <-c.msg:
			var r response
			err = proto.Unmarshal(buf, &r)
			if err != nil {
				log.Print(err)
				continue
			}

			if r.Tag == nil {
				log.Printf("nil tag: %# v", pretty.Formatter(r))
				continue
			}
			t := txns[*r.Tag]
			if t == nil {
				log.Printf("unexpected: %# v", pretty.Formatter(r))
				continue
			}

			delete(txns, *r.Tag)
			t.resp = &r
			t.done <- true
		case err = <-errch:
			goto error
		case <-c.stop:
			err = ErrClosed
			goto error
		}
	}

error:
	c.err = err
	for _, t := range txns {
		t.err = err
		t.done <- true
	}
	c.conn.Close()
	close(c.stopped)
}

func (c *Conn) readAll(errch chan error) {
	for {
		buf, err := c.read()
		if err != nil {
			errch <- err
			return
		}

		c.msg <- buf
	}
}

func (c *Conn) read() ([]byte, error) {
	var size int32
	err := binary.Read(c.conn, binary.BigEndian, &size)
	if err != nil {
		return nil, err
	}

	buf := make([]byte, size)
	_, err = io.ReadFull(c.conn, buf)
	if err != nil {
		return nil, err
	}

	return buf, nil
}

func (c *Conn) write(buf []byte) error {
	err := binary.Write(c.conn, binary.BigEndian, int32(len(buf)))
	if err != nil {
		return err
	}

	_, err = c.conn.Write(buf)
	return err
}

// Attempts access to the store
func (c *Conn) Access(token string) error {
	var t txn
	t.req.Verb = request_ACCESS.Enum()
	t.req.Value = []byte(token)
	return c.call(&t)
}

// Sets the contents of file to body, if it hasn't been modified since oldRev.
func (c *Conn) Set(file string, oldRev int64, body []byte) (newRev int64, err error) {
	var t txn
	t.req.Verb = request_SET.Enum()
	t.req.Path = &file
	t.req.Value = body
	t.req.Rev = &oldRev

	err = c.call(&t)
	if err != nil {
		return
	}

	return t.resp.GetRev(), nil
}

// Deletes file, if it hasn't been modified since rev.
func (c *Conn) Del(file string, rev int64) error {
	var t txn
	t.req.Verb = request_DEL.Enum()
	t.req.Path = &file
	t.req.Rev = &rev
	return c.call(&t)
}

func (c *Conn) Nop() error {
	var t txn
	t.req.Verb = request_NOP.Enum()
	return c.call(&t)
}

// Returns the body and revision of the file at path,
// as of store revision *rev.
// If rev is nil, uses the current state.
func (c *Conn) Get(file string, rev *int64) ([]byte, int64, error) {
	var t txn
	t.req.Verb = request_GET.Enum()
	t.req.Path = &file
	t.req.Rev = rev

	err := c.call(&t)
	if err != nil {
		return nil, 0, err
	}

	return t.resp.Value, t.resp.GetRev(), nil
}

// Getdir reads up to lim names from dir, at revision rev, into an array.
// Names are read in lexicographical order, starting at position off.
// A negative lim means to read until the end.
func (c *Conn) Getdir(dir string, rev int64, off, lim int) (names []string, err error) {
	for lim != 0 {
		var t txn
		t.req.Verb = request_GETDIR.Enum()
		t.req.Rev = &rev
		t.req.Path = &dir
		t.req.Offset = proto.Int32(int32(off))
		err = c.call(&t)
		if err, ok := err.(*Error); ok && err.Err == ErrRange {
			return names, nil
		}
		if err != nil {
			return nil, err
		}
		names = append(names, *t.resp.Path)
		off++
		lim--
	}
	return
}

// Getdirinfo reads metadata for up to lim files from dir, at revision rev,
// into an array.
// Files are read in lexicographical order, starting at position off.
// A negative lim means to read until the end.
// Getdirinfo returns the array and an error, if any.
func (c *Conn) Getdirinfo(dir string, rev int64, off, lim int) (a []FileInfo, err error) {
	names, err := c.Getdir(dir, rev, off, lim)
	if err != nil {
		return nil, err
	}

	if dir != "/" {
		dir += "/"
	}
	a = make([]FileInfo, len(names))
	for i, name := range names {
		var fp *FileInfo
		fp, err = c.Statinfo(rev, dir+name)
		if err != nil {
			a[i].Name = name
		} else {
			a[i] = *fp
		}
	}
	return
}

// Statinfo returns metadata about the file or directory at path,
// in revision *storeRev. If storeRev is nil, uses the current
// revision.
func (c *Conn) Statinfo(rev int64, path string) (f *FileInfo, err error) {
	f = new(FileInfo)
	f.Len, f.Rev, err = c.Stat(path, &rev)
	if err != nil {
		return nil, err
	}
	if f.Rev == missing {
		return nil, ErrNoEnt
	}
	f.Name = basename(path)
	f.IsSet = true
	f.IsDir = f.Rev == dir
	return f, nil
}

// Stat returns metadata about the file or directory at path,
// in revision *storeRev. If storeRev is nil, uses the current
// revision.
func (c *Conn) Stat(path string, storeRev *int64) (len int, fileRev int64, err error) {
	var t txn
	t.req.Verb = request_STAT.Enum()
	t.req.Path = &path
	t.req.Rev = storeRev

	err = c.call(&t)
	if err != nil {
		return 0, 0, err
	}

	return int(t.resp.GetLen()), t.resp.GetRev(), nil
}

// Walk reads up to lim entries matching glob, in revision rev, into an array.
// Entries are read in lexicographical order, starting at position off.
// A negative lim means to read until the end.
// Conn.Walk will be removed in a future release. Use Walk instead.
func (c *Conn) Walk(glob string, rev int64, off, lim int) (info []Event, err error) {
	for lim != 0 {
		var t txn
		t.req.Verb = request_WALK.Enum()
		t.req.Rev = &rev
		t.req.Path = &glob
		t.req.Offset = proto.Int32(int32(off))
		err = c.call(&t)
		if err, ok := err.(*Error); ok && err.Err == ErrRange {
			return info, nil
		}
		if err != nil {
			return nil, err
		}
		info = append(info, Event{
			*t.resp.Rev,
			*t.resp.Path,
			t.resp.Value,
			*t.resp.Flags,
		})
		off++
		lim--
	}
	return
}

// Waits for the first change, on or after rev, to any file matching glob.
func (c *Conn) Wait(glob string, rev int64) (ev Event, err error) {
	var t txn
	t.req.Verb = request_WAIT.Enum()
	t.req.Path = &glob
	t.req.Rev = &rev

	err = c.call(&t)
	if err != nil {
		return
	}

	ev.Rev = *t.resp.Rev
	ev.Path = *t.resp.Path
	ev.Body = t.resp.Value
	ev.Flag = *t.resp.Flags & (set | del)
	return
}

// Waits for the first change, on or after rev, to any file matching glob,
// within the specific time expressed as time.Duration
func (c *Conn) WaitTimeout(glob string, rev int64, timeout time.Duration) (ev Event, err error) {
	var timer *time.Timer
	if timeout > 0 {
		timer = time.AfterFunc(timeout, func() {
			c.err = ErrWaitTimeout
			c.stopped <- true
		})
	}
	ev, err = c.Wait(glob, rev)
	if timer != nil {
		timer.Stop()
	}
	return
}

// Rev returns the current revision of the store.
func (c *Conn) Rev() (int64, error) {
	var t txn
	t.req.Verb = request_REV.Enum()

	err := c.call(&t)
	if err != nil {
		return 0, err
	}

	return *t.resp.Rev, nil
}

// Self returns the node's identifier
func (c *Conn) Self() ([]byte, error) {
	var t txn
	t.req.Verb = request_SELF.Enum()

	err := c.call(&t)
	if err != nil {
		return nil, err
	}

	return t.resp.Value, nil
}