예제 #1
0
func (cs ClusterService) saveCluster() {
	cs.clusterMutex.Lock()

	tempPath := fmt.Sprintf("%s/%d", os.TempDir(), time.Nanoseconds())
	file, err := os.Create(tempPath)
	if err != nil {
		log.Fatal("Couldn't open temp cluster data file", err)
	}

	typedFile := typedio.NewWriter(file)

	typedFile.WriteInt64(cs.clusterVersion) // cluster version
	cs.diskVerson = cs.clusterVersion

	typedFile.WriteUint16(cs.cluster.Nodes.Count()) // nodes count
	for node := range cs.cluster.Nodes.Iter() {
		node.Serialize(typedFile)
	}

	file.Close()

	err = os.Rename(tempPath, cs.clsDataPath)
	if err != nil {
		log.Fatal("Couldn't rename %s to %s", tempPath, cs.clsDataPath)
	}

	cs.clusterMutex.Unlock()
}
예제 #2
0
파일: segment.go 프로젝트: appaquet/gostore
func createSegment(dataDir string, token_from, token_to Token, position uint64) *segment {
	seg := &segment{
		nextSegments:  make([]*segment, 0),
		prevSegments:  make([]*segment, 0),
		tokens:        TokenRange{token_from, token_to},
		positionStart: position,
		positionEnd:   position,
		lock:          new(sync.Mutex),
	}

	var err os.Error

	filePath := fmt.Sprintf("%s/%016X_%04X_%04X.seg", dataDir, position, token_from, token_to)
	seg.fd, err = os.Create(filePath)
	if err != nil {
		log.Fatal("Couldn't open segment %s: %s", filePath, err)
	}
	seg.typedFd = typedio.NewReadWriter(seg.fd)
	seg.writable = true

	seg.buf = bufio.NewWriter(seg.fd)
	seg.typedBuf = typedio.NewWriter(seg.buf)

	return seg
}
예제 #3
0
파일: message.go 프로젝트: appaquet/gostore
func (r *Message) writeMessage(writer io.Writer) (err os.Error) {
	twriter := typedio.NewWriter(writer)

	err = twriter.WriteUint16(r.Id) // message id
	if err != nil {
		return
	}

	// prepare flags
	var flags byte
	if r.InitId != 0 {
		flags = flags | prm_has_init_msg_id
	}

	if r.Type == T_DATA {
		flags = flags | prm_is_data
	}

	if r.srcNodeAdhoc {
		flags = flags | prm_src_node_adhoc
	}

	if r.middleNodePresent {
		flags = flags | prm_has_middle_node
	}

	if r.middleNodeAdhoc {
		flags = flags | prm_middle_node_adhoc
	}

	err = twriter.WriteUint8(flags) // flags
	if err != nil {
		return
	}

	if r.InitId != 0 {
		err = twriter.WriteUint16(r.InitId) // initial message id
		if err != nil {
			return
		}
	}

	err = twriter.WriteUint8(r.ServiceId) // service id
	if err != nil {
		return
	}

	msgSize := uint16(r.Message.Size)
	err = twriter.WriteUint16(msgSize) // message size
	if err != nil {
		return
	}

	if r.Type == T_DATA {
		twriter.WriteInt64(r.DataSize) // data size
		if err != nil {
			return
		}
	}

	if r.srcNodeAdhoc { // source node information
		err = twriter.WriteString(r.srcNodeAdr.String()) // addr
		if err != nil {
			return
		}

		err = twriter.WriteUint16(r.srcNodeTcpPort) // tcp port
		if err != nil {
			return
		}

		err = twriter.WriteUint16(r.srcNodeUdpPort) // udp port
		if err != nil {
			return
		}

	} else {
		err = twriter.WriteUint16(r.srcNodeId) // node id
		if err != nil {
			return
		}
	}

	if r.middleNodePresent { // middle node information
		if r.middleNodeAdhoc {
			err = twriter.WriteString(r.middleNodeAdr.String()) // addr
			if err != nil {
				return
			}

			err = twriter.WriteUint16(r.middleNodeTcpPort) // tcp port
			if err != nil {
				return
			}

			err = twriter.WriteUint16(r.middleNodeUdpPort) // udp port
			if err != nil {
				return
			}

		} else {
			err = twriter.WriteUint16(r.middleNodeId) // node id
			if err != nil {
				return
			}
		}
	}

	err = twriter.WriteUint8(r.FunctionId) // function id
	if err != nil {
		return
	}

	// Write message
	r.Message.Seek(0, 0)
	w, err := io.Copyn(writer, r.Message, r.Message.Size) // message

	if err != nil {
		log.Error("Couldn't write message message to writer: %s\n", err)
		return err
	}

	if w != int64(msgSize) {
		log.Error("Couldn't write the whole message message to write: written %d out of %d\n", w, msgSize)
		return os.NewError("Message write truncated")
	}

	// Write data
	if r.Type == T_DATA {
		io.Copyn(writer, r.Data, r.DataSize) // data
	}

	return nil
}