Ejemplo n.º 1
0
func (as *AirlineServer) initTrans(request *Request) {
	args := request.content.value.(*airlineproto.TranArgs)
	tranID := args.TranID
	flightIDs := strings.Split(args.FlightID, ":")
	amount := args.Amount
	pptList := args.PptList
	tm := args.TM
	decision := transaction.COMMIT
	log.Printf("[%s:%s] Receive Init T(%s)", as.airlineID, as.myhostport, tranID)
	logList := bufi.NewBuf()
	for _, flightID := range flightIDs {
		info, exist := as.getFlightInfo(flightID)
		if exist == false || info.Slot < amount {
			decision = transaction.ABORT
			if exist == true {
				// lsplog.Vlogf(5, "[%s:%s] Slot %d >= Amount %d", as.airlineID, as.myhostport, info.Slot, amount)
			}
			break
		} else {
			getLock := as.getFlightLock(flightID)
			if getLock == false {
				// @@ defer or refuse??
				decision = transaction.ABORT
				// lsplog.Vlogf(5, "could not get lock")
				break
			} else {
				log := &transLog{UPDATE, tranID, flightID, info.Slot, info.Slot - amount, pptList, tm}
				logList.Insert(log)
			}
		}
	}

	if decision == transaction.COMMIT {
		// TODO: store log to persistent storage

	} else {
		// TODO: store log to persistent storage
		log.Printf("[%s:%s] Decision for T(%s): %d", as.airlineID, as.myhostport, tranID, decision)
	}
	conn, err := as.lib_conn.GetServerAt(tm)
	if err != nil {
		return
	}

	if request.own == true {
		log.Printf("[%s:%s] Vote %d for T(%s)", as.airlineID, as.myhostport, decision, tranID)
		newArgs := &airlineproto.TranArgs{transaction.TRANS_RESPONSE,
			tranID, args.FlightID, 0, nil, decision, as.myhostport}
		var reply airlineproto.TranReply
		err = conn.Call("TranRPC.TransResponse", newArgs, &reply)
	}

	if decision == transaction.COMMIT {
		as.logBuf[tranID] = logList
		go as.transTimer(tranID)
	}
}
Ejemplo n.º 2
0
func (la *Libairline) MakeBooking(flightids []string, isRemove bool) error {
	//TODO: Handle remove
	orderQuantity := 1

	if isRemove {
		orderQuantity = -1
	}

	orderGrpMap := make(map[string]*bufi.Buf)

	for _, v := range flightids {
		airlineId := tribproto.GetAirlineFromFlightId(v)
		orderList, exist := orderGrpMap[airlineId]
		if exist == false {
			orderList = bufi.NewBuf()
		}
		orderList.Insert(v)
		orderGrpMap[airlineId] = orderList
	}

	orders := make([]tranlayerproto.Order, len(orderGrpMap))
	i := 0
	for airlineId, orderList := range orderGrpMap {
		flightIds := ""
		for !orderList.Empty() {
			flightId := orderList.Remove().(string)
			if flightIds == "" {
				flightIds = flightId
			} else {
				flightIds = fmt.Sprintf("%s:%s", flightIds, flightId)
			}
		}

		order := tranlayerproto.Order{airlineId, flightIds, orderQuantity}
		orders[i] = order
		i++
	}
	err := la.lib_tranlayer.BookingFlights(orders)
	if lsplog.CheckReport(2, err) {
		return err
	}

	return nil
}
Ejemplo n.º 3
0
func (tl *TranLayer) initTrans(request *Request) {
	// initialize old transaction
	oldTransaction := &OldTransaction{}

	// create participant list
	orders := request.content.value.([]*tranOrder)
	pptList := make([]string, len(orders))
	for i, order := range orders {
		pptList[i] = order.pptID
	}
	oldTransaction.pptList = pptList
	// send initialize transaction msg to participants
	// create active transaction
	activeTrans := &activeTransMapEle{}
	activeTrans.decision = transaction.NOT_DECIDED
	activeTrans.finished = 0
	pptMap := make(map[string](*rpc.Client))
	commitGrp := make(map[string](int))
	transID := request.content.key
	orderList := bufi.NewBuf()
	log.Printf("[%s] Init T(%s)", tl.myhostport, transID)
	for _, order := range orders {
		orderList.Insert(order)
		//s := []string{order.pptID, order.flightID}
		//transKey := strings.Join(s, "#")
		transKey := order.pptID
		pptMap[transKey] = order.conn
		commitGrp[transKey] = transaction.NOT_DECIDED
	}

	oldTransaction.orderList = orderList
	tl.oldTransactionMap[transID] = oldTransaction

	activeTrans.pptMap = pptMap
	activeTrans.commitGrp = commitGrp
	tl.activeTransMap[transID] = activeTrans

	// TODO: store log to persistent storage

	tl.deferTransMap[transID] = request.replyc
}