Ejemplo n.º 1
0
func (s *testIoPipeSuite) TestWriteAfterWriterClose(c *C) {
	r, w := Pipe()

	ss := "hello"

	errs := make(chan error)

	go func() {
		_, err := ioutils.WriteFull(w, []byte(ss))
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)

		_, err = w.Write([]byte("world"))
		errs <- err
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(string(buf[:n]), Equals, ss)

	err = <-errs
	c.Assert(errors2.ErrorEqual(err, io.ErrClosedPipe), Equals, true)
	c.Assert(r.Close(), IsNil)
}
Ejemplo n.º 2
0
func (s *testBytesizeSuite) TestBytesizeError(c *C) {
	var err error
	_, err = Parse("--1")
	c.Assert(errors2.ErrorEqual(err, ErrBadBytesize), Equals, true)
	_, err = Parse("hello world")
	c.Assert(errors2.ErrorEqual(err, ErrBadBytesize), Equals, true)
	_, err = Parse("123.132.32")
	c.Assert(errors2.ErrorEqual(err, ErrBadBytesize), Equals, true)
}
Ejemplo n.º 3
0
func (db *GoLevelDB) Get(key []byte) ([]byte, error) {
	value, err := db.lvdb.Get(key, db.ropt)
	if errors2.ErrorEqual(err, leveldb.ErrNotFound) {
		return nil, nil
	}
	return value, errors.Trace(err)
}
Ejemplo n.º 4
0
Archivo: main.go Proyecto: CowLeo/qdb
func (h *Handler) run() error {
	log.Infof("open listen address '%s' and start service", h.l.Addr())

	for {
		if nc, err := h.l.Accept(); err != nil {
			return errors.Trace(err)
		} else {
			h.counters.clientsAccepted.Add(1)
			go func() {
				h.counters.clients.Add(1)
				defer h.counters.clients.Sub(1)

				c := newConn(nc, h, h.config.ConnTimeout)

				log.Infof("new connection: %s", c)
				if err := c.serve(h); err != nil {
					if errors2.ErrorEqual(err, io.EOF) {
						log.Infof("connection lost: %s [io.EOF]", c)
					} else {
						log.Warningf("connection lost: %s, err = %s", c, err)
					}
				} else {
					log.Infof("connection exit: %s", c)
				}
			}()
		}
	}
	return nil
}
Ejemplo n.º 5
0
func (s *testIoPipeSuite) TestPipeReadClose(c *C) {
	for _, u := range pipeTests {
		r, w := Pipe()
		ch := make(chan int, 1)

		if u.async {
			go s.delayClose(c, w, ch, u)
		} else {
			s.delayClose(c, w, ch, u)
		}

		buf := make([]byte, 64)
		n, err := r.Read(buf)
		<-ch

		expect := u.err
		if expect == nil {
			expect = io.EOF
		}

		c.Assert(errors2.ErrorEqual(err, expect), Equals, true)
		c.Assert(n, Equals, 0)
		c.Assert(r.Close(), IsNil)
	}
}
Ejemplo n.º 6
0
func (s *testIoPipeSuite) testPipe2(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	cc := 1024 * 128
	ss := "Hello world!!"

	go func() {
		for i := 0; i < cc; i++ {
			m := fmt.Sprintf("[%d]%s ", i, ss)
			_, err := ioutils.WriteFull(w, []byte(m))
			c.Assert(err, IsNil)
		}
		c.Assert(w.Close(), IsNil)
	}()

	time.Sleep(time.Millisecond * 100)

	buf := make([]byte, len(ss)*cc*2)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)

	buf = buf[:n]
	for i := 0; i < cc; i++ {
		m := fmt.Sprintf("[%d]%s ", i, ss)
		c.Assert(len(buf) >= len(m), Equals, true)
		c.Assert(string(buf[:len(m)]), Equals, m)
		buf = buf[len(m):]
	}

	c.Assert(len(buf), Equals, 0)
	c.Assert(r.Close(), IsNil)
}
Ejemplo n.º 7
0
func (sp *Snapshot) Get(key []byte) ([]byte, error) {
	value, err := sp.snap.Get(key, sp.ropt)
	if errors2.ErrorEqual(err, leveldb.ErrNotFound) {
		return nil, nil
	}

	return value, errors.Trace(err)
}
Ejemplo n.º 8
0
Archivo: string.go Proyecto: CowLeo/qdb
// SET key value [EX seconds] [PX milliseconds] [NX|XX]
func SetCmd(s Session, args [][]byte) (redis.Resp, error) {
	if err := s.Store().Set(s.DB(), args); err != nil && errors2.ErrorNotEqual(err, store.ErrSetAborted) {
		return toRespError(err)
	} else if errors2.ErrorEqual(err, store.ErrSetAborted) {
		return redis.NewBulkBytes(nil), nil
	} else {
		return redis.NewString("OK"), nil
	}
}
Ejemplo n.º 9
0
func (s *testIoPipeSuite) TestPipeReadClose2(c *C) {
	r, w := Pipe()
	ch := make(chan int, 1)

	go s.delayClose(c, r, ch, pipeTest{})

	n, err := r.Read(make([]byte, 64))
	<-ch

	c.Assert(errors2.ErrorEqual(err, io.ErrClosedPipe), Equals, true)

	c.Assert(n, Equals, 0)
	c.Assert(w.Close(), IsNil)
}
Ejemplo n.º 10
0
func (s *testIoPipeSuite) TestWriteNil(c *C) {
	r, w := Pipe()

	go func() {
		_, err := w.Write(nil)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(n, Equals, 0)
	c.Assert(r.Close(), IsNil)
}
Ejemplo n.º 11
0
func (s *testIoPipeSuite) testPipe3(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	ch := make(chan int)

	size := 4096

	go func() {
		buf := make([]byte, size)
		for {
			n, err := r.Read(buf)

			if errors2.ErrorEqual(err, io.EOF) {
				break
			}

			c.Assert(err, IsNil)
			ch <- n
		}

		c.Assert(r.Close(), IsNil)
		ch <- 0
	}()

	go func() {
		buf := make([]byte, size)
		for i := 1; i < size; i++ {
			n, err := ioutils.WriteFull(w, buf[:i])
			c.Assert(err, IsNil)
			c.Assert(n, Equals, i)
		}
		c.Assert(w.Close(), IsNil)
	}()

	sum := 0
	for i := 1; i < size; i++ {
		sum += i
	}
	for {
		n := <-ch
		if n == 0 {
			break
		}
		sum -= n
	}

	c.Assert(sum, Equals, 0)
}
Ejemplo n.º 12
0
func (s *testIoPipeSuite) testPipe4(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	key := []byte("spinlock aes-128")

	block := aes.BlockSize
	count := 1024 * 1024 * 128 / block

	go func() {
		buf := make([]byte, count*block)
		m, err := aes.NewCipher(key)
		c.Assert(err, IsNil)

		for i := 0; i < len(buf); i++ {
			buf[i] = byte(i)
		}

		e := cipher.NewCBCEncrypter(m, make([]byte, block))
		e.CryptBlocks(buf, buf)

		n, err := ioutils.WriteFull(w, buf)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
		c.Assert(n, Equals, len(buf))
	}()

	buf := make([]byte, count*block)
	m, err := aes.NewCipher(key)
	c.Assert(err, IsNil)

	_, err = ioutils.ReadFull(r, buf)
	c.Assert(err, IsNil)

	e := cipher.NewCBCDecrypter(m, make([]byte, block))
	e.CryptBlocks(buf, buf)

	for i := 0; i < len(buf); i++ {
		// make gocheck faster
		if buf[i] != byte(i) {
			c.Assert(buf[i], Equals, byte(i))
		}
	}

	_, err = ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(r.Close(), IsNil)
}
Ejemplo n.º 13
0
func (s *testIoPipeSuite) testPipe1(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	ss := "Hello world!!"

	go func(data []byte) {
		_, err := ioutils.WriteFull(w, data)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
	}([]byte(ss))

	buf := make([]byte, 64)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(n, Equals, len(ss))
	c.Assert(string(buf[:n]), Equals, ss)
	c.Assert(r.Close(), IsNil)
}
Ejemplo n.º 14
0
// SETNX key value
func (s *Store) SetNX(db uint32, args [][]byte) (int64, error) {
	if len(args) != 2 {
		return 0, errArguments("len(args) = %d, expect = 2", len(args))
	}

	key := args[0]
	value := args[1]

	err := s.Set(db, [][]byte{key, value, []byte("NX")})

	if err != nil {
		// key exists
		if errors2.ErrorEqual(err, ErrSetAborted) {
			return 0, nil
		} else {
			return 0, err
		}
	}

	return 1, nil
}
Ejemplo n.º 15
0
// travel zset in lex range, call f in every iteration.
func (o *zsetRow) travelInLexRange(s *Store, r *lexRangeSpec, f func(o *zsetRow) error) error {
	it := s.getIterator()
	defer s.putIterator(it)

	o.Score = math.Inf(-1)
	o.Member = r.Min

	it.SeekTo(o.IndexKey())
	prefixKey := o.IndexKeyPrefix()
	for ; it.Valid(); it.Next() {
		key := it.Key()
		if !bytes.HasPrefix(key, prefixKey) {
			return nil
		}

		key = key[len(prefixKey):]

		if err := o.ParseIndexKeySuffix(key); err != nil {
			return errors.Trace(err)
		}

		if r.InRange(o.Member) {
			if err := f(o); errors2.ErrorEqual(err, errTravelBreak) {
				return nil
			} else if err != nil {
				return errors.Trace(err)
			}
		} else if !r.LteMax(o.Member) {
			return nil
		}
	}

	if err := it.Error(); err != nil {
		return errors.Trace(err)
	}
	return nil
}
Ejemplo n.º 16
0
// reverse travel zset in lex range, call f in every iteration.
func (o *zsetRow) reverseTravelInLexRange(s *Store, r *lexRangeSpec, f func(o *zsetRow) error) error {
	it := s.getIterator()
	defer s.putIterator(it)

	prefixKey := o.IndexKeyPrefix()

	o.seekToLastInLexRange(it, r)

	for ; it.Valid(); it.Prev() {
		key := it.Key()
		if !bytes.HasPrefix(key, prefixKey) {
			return nil
		}

		key = key[len(prefixKey):]

		if err := o.ParseIndexKeySuffix(key); err != nil {
			return errors.Trace(err)
		}

		if r.InRange(o.Member) {
			if err := f(o); errors2.ErrorEqual(err, errTravelBreak) {
				return nil
			} else if err != nil {
				return errors.Trace(err)
			}
		} else if !r.GteMin(o.Member) {
			return nil
		}
	}

	if err := it.Error(); err != nil {
		return errors.Trace(err)
	}
	return nil
}
Ejemplo n.º 17
0
// migrate multi slots
func (t *MigrateTask) run() error {
	// create zk conn on demand
	t.coordConn = CreateCoordConn()
	defer t.coordConn.Close()

	to := t.NewGroupId
	t.Status = MIGRATE_TASK_MIGRATING
	for slotId := t.FromSlot; slotId <= t.ToSlot; slotId++ {
		err := t.migrateSingleSlot(slotId, to)
		if errors2.ErrorEqual(err, ErrStopMigrateByUser) {
			log.Info("stop migration job by user")
			break
		} else if err != nil {
			log.Error(err)
			t.Status = MIGRATE_TASK_ERR
			return err
		}
		t.Percent = (slotId - t.FromSlot + 1) * 100 / (t.ToSlot - t.FromSlot + 1)
		log.Info("total percent:", t.Percent)
	}
	t.Status = MIGRATE_TASK_FINISHED
	log.Info("migration finished")
	return nil
}
Ejemplo n.º 18
0
func (s *testIoPipeSuite) TestPipeWriteClose(c *C) {
	for _, u := range pipeTests {
		r, w := Pipe()
		ch := make(chan int, 1)

		if u.async {
			go s.delayClose(c, r, ch, u)
		} else {
			s.delayClose(c, r, ch, u)
		}
		<-ch

		n, err := ioutils.WriteFull(w, []byte("hello, world"))
		expect := u.err
		if expect == nil {
			expect = io.ErrClosedPipe
		}

		c.Assert(errors2.ErrorEqual(err, expect), Equals, true)

		c.Assert(n, Equals, 0)
		c.Assert(w.Close(), IsNil)
	}
}