Exemplo n.º 1
0
func DataPull(txn *cheshire.Txn) {
	part, ok := txn.Params().GetInt("partition")
	if !ok {
		cheshire.SendError(txn, 406, fmt.Sprintf("partition param is manditory"))
		return
	}
	dataChan := make(chan *dynmap.DynMap, 10)
	finishedChan := make(chan int)
	errorChan := make(chan error)
	go func() {
		for {
			select {
			case data := <-dataChan:
				//send a data packet
				res := cheshire.NewResponse(txn)
				res.SetTxnStatus("continue")
				res.Put("data", data)
				txn.Write(res)
			case <-finishedChan:
				res := cheshire.NewResponse(txn)
				res.SetTxnStatus("complete")
				txn.Write(res)
				return
			case err := <-errorChan:
				cheshire.SendError(txn, 406, fmt.Sprintf("Unable to unlock (%s)", err))
				return
			}
		}
	}()
	manager.partitioner.Data(part, dataChan, finishedChan, errorChan)
}
Exemplo n.º 2
0
func ServiceGet(txn *cheshire.Txn) {
	routerTable, ok := Servs.RouterTable(txn.Params().MustString("service", ""))
	if !ok {
		cheshire.SendError(txn, 406, "Service param missing or service not found")
		return
	}
	res := cheshire.NewResponse(txn)
	res.Put("router_table", routerTable.ToDynMap())
	txn.Write(res)
}
Exemplo n.º 3
0
func Service(txn *cheshire.Txn) {
	//create a context map to be passed to the template
	context := make(map[string]interface{})

	service, ok := Servs.RouterTable(txn.Params().MustString("service", ""))
	context["service"] = service
	if !ok {
		cheshire.Flash(txn, "error", fmt.Sprintf("Cant find service"))
		cheshire.Redirect(txn, "/index")
		return
	}
	cheshire.RenderInLayout(txn, "/router_table.html", "/template.html", context)
}
Exemplo n.º 4
0
func Unlock(txn *cheshire.Txn) {
	partition, ok := txn.Params().GetInt("partition")
	if !ok {
		cheshire.SendError(txn, 406, fmt.Sprintf("partition param missing"))
		return
	}

	err := manager.UnlockPartition(partition)
	if err != nil {
		//now send back an error
		cheshire.SendError(txn, 406, fmt.Sprintf("Unable to lock partitions (%s)", err))
		return
	}
	response := cheshire.NewResponse(txn)
	txn.Write(response)
}
Exemplo n.º 5
0
func NewService(txn *cheshire.Txn) {
	log.Println(txn.Params())

	name, ok := txn.Params().GetString("service-name")
	if !ok {
		cheshire.Flash(txn, "error", "Service Name is manditory")
	}

	err := Servs.NewRouterTable(name, 512, 2)
	if err != nil {
		cheshire.Flash(txn, "error", fmt.Sprintf("%s", err))
	} else {
		cheshire.Flash(txn, "success", "successfully created router table")
	}
	cheshire.Redirect(txn, "/index")
}
Exemplo n.º 6
0
func SetRouterTable(txn *cheshire.Txn) {
	rtmap, ok := txn.Params().GetDynMap("router_table")
	if !ok {
		cheshire.SendError(txn, 406, "No router_table")
		return
	}

	rt, err := ToRouterTable(rtmap)
	if err != nil {
		cheshire.SendError(txn, 406, fmt.Sprintf("Unparsable router table (%s)", err))
		return
	}

	_, err = manager.SetRouterTable(rt)
	if err != nil {
		cheshire.SendError(txn, 406, fmt.Sprintf("Unable to set router table (%s)", err))
		return
	}
	response := cheshire.NewResponse(txn)
	txn.Write(response)
}
Exemplo n.º 7
0
// Creates a new shard.  Does not register any partitions to it, unless the router table has no entries. in which case this
// gets all the partitions
func ShardNew(txn *cheshire.Txn) {
	routerTable, ok := Servs.RouterTable(txn.Params().MustString("service", ""))

	if !ok {
		cheshire.SendError(txn, 406, "Service param missing or service not found")
		return
	}

	Servs.Logger.Printf("Creating new shard for service: %s", routerTable.Service)

	address, ok := txn.Params().GetString("address")
	if !ok {
		cheshire.SendError(txn, 406, "address param missing")
		return
	}

	jsonPort, ok := txn.Params().GetInt("json_port")
	if !ok {
		cheshire.SendError(txn, 406, "json_port param missing")
		return
	}

	httpPort, ok := txn.Params().GetInt("http_port")
	if !ok {
		cheshire.SendError(txn, 406, "http_port param missing")
		return
	}

	entry := &shards.RouterEntry{
		Address:    address,
		JsonPort:   jsonPort,
		HttpPort:   httpPort,
		Partitions: make([]int, 0),
	}

	//check if we can connect!
	Servs.Logger.Printf("Attempting to connect to new entry...")
	// _, _, err := EntryCheckin(routerTable, entry)
	// if err != nil {
	//     cheshire.SendError(txn, 406, fmt.Sprintf("Unable to contact %s:%d Error(%s)", entry.Address, entry.HttpPort, err))
	//     return
	// }
	Servs.Logger.Printf("Success!")

	if len(routerTable.Entries) == 0 {
		totalPartitions, ok := txn.Params().GetInt("total_partitions")
		if !ok {
			cheshire.SendError(txn, 406, "total_partitions param is manditory for the first entry")
			return
		}

		//first entry, giving it all the partitions
		Servs.Logger.Printf("First Entry! giving it all %d partitions", totalPartitions)
		partitions := make([]int, 0)
		for p := 0; p < totalPartitions; p++ {
			partitions = append(partitions, p)
		}
		entry.Partitions = partitions
	} else {
		// not the first entry,
	}

	routerTable, err := routerTable.AddEntries(entry)
	if err != nil {
		cheshire.SendError(txn, 501, fmt.Sprintf("Error on add entry %s", err))
		return
	}

	Servs.Logger.Printf("Successfully created new entry: %s", entry.Id())

	Servs.SetRouterTable(routerTable)

	res := cheshire.NewResponse(txn)
	res.Put("router_table", routerTable.ToDynMap())
	txn.Write(res)
}