예제 #1
0
/*
此api实现复制写入操作 步骤如下:
1.先写入本地
2. 如果需要复制则 将消息投递replicate到master
3. 如果2操作成功完成则返回
4. 如果2失败,则删除本地副本并投递删除消息到master
5. 返回4的结果

同理删除


*/
func ReplicatedWrite(masterNode string, s *storage.Store, volumeId storage.VolumeId, needle *storage.Needle, r *http.Request) (size uint32, errorStatus string) {
	ret, err := s.Write(volumeId, needle)
	needToReplicate := !s.HasVolume(volumeId)
	if err != nil {
		errorStatus = "Failed to write to local disk (" + err.Error() + ")"
	} else if ret > 0 {  //写入成功
		needToReplicate = needToReplicate || s.GetVolume(volumeId).NeedToReplicate()
	} else {
		errorStatus = "Failed to write to local disk"
	}

	//??? --lewgun
	if !needToReplicate && ret > 0 {
		needToReplicate = s.GetVolume(volumeId).NeedToReplicate()
	}
	if needToReplicate { //send to other replica locations
		if r.FormValue("type") != "replicate" {  //只有最原始的才会执行distributedOperation

			//向master复制
			if !distributedOperation(masterNode,
				    s,
						volumeId,
						func(location operation.Location) bool {
								_, err := operation.Upload(
								"http://"+location.Url+r.URL.Path+"?type=replicate&ts="+strconv.FormatUint(needle.LastModified, 10),
					 			string(needle.Name),
					 			bytes.NewReader(needle.Data),
					 			needle.IsGzipped(),
					 			string(needle.Mime))

								return err == nil
			 })
			{
				ret = 0
				errorStatus = "Failed to write to replicas for volume " + volumeId.String()
			}// if !distributedOperation(masterNode,

		} //	if r.FormValue("type") != "replicate" {

	}

	//复制失败,删除本地副本
	if errorStatus != "" {
		if _, err = s.Delete(volumeId, needle); err != nil {
			errorStatus += "\nCannot delete " + strconv.FormatUint(needle.Id, 10) + " from " +
				volumeId.String() + ": " + err.Error()
		} else {
			distributedOperation(
				masterNode,
				s,
				volumeId,
				func(location operation.Location) bool {
					return nil == util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate")
			 }
			) // distributedOperation(...)
		} // 	if errorStatus != "" {
	}
	size = ret
	return
}
예제 #2
0
func ReplicatedDelete(masterNode string, store *storage.Store, volumeId storage.VolumeId, n *storage.Needle, r *http.Request) (ret uint32) {
	ret, err := store.Delete(volumeId, n)
	if err != nil {
		glog.V(0).Infoln("delete error:", err)
		return
	}

	needToReplicate := !store.HasVolume(volumeId)
	if !needToReplicate && ret > 0 {
		needToReplicate = store.GetVolume(volumeId).NeedToReplicate()
	}
	if needToReplicate { //send to other replica locations
		if r.FormValue("type") != "replicate" {
			if !distributedOperation(masterNode, store, volumeId, func(location operation.Location) bool {
				return nil == util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate")
			}) {
				ret = 0
			}
		}
	}
	return
}