本文整理汇总了Golang中github.com/mesos/mesos-go/mesosutil.FilterResources函数的典型用法代码示例。如果您正苦于以下问题:Golang FilterResources函数的具体用法?Golang FilterResources怎么用?Golang FilterResources使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FilterResources函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ResourceOffers
func (sched *ExampleScheduler) ResourceOffers(driver sched.SchedulerDriver, offers []*mesos.Offer) {
for _, offer := range offers {
cpuResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == "cpus"
})
cpus := 0.0
for _, res := range cpuResources {
cpus += res.GetScalar().GetValue()
}
memResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == "mem"
})
mems := 0.0
for _, res := range memResources {
mems += res.GetScalar().GetValue()
}
log.Infoln("Received Offer <", offer.Id.GetValue(), "> with cpus=", cpus, " mem=", mems)
remainingCpus := cpus
remainingMems := mems
var tasks []*mesos.TaskInfo
for sched.tasksLaunched < sched.totalTasks &&
CPUS_PER_TASK <= remainingCpus &&
MEM_PER_TASK <= remainingMems {
sched.tasksLaunched++
taskId := &mesos.TaskID{
Value: proto.String(strconv.Itoa(sched.tasksLaunched)),
}
task := &mesos.TaskInfo{
Name: proto.String("go-task-" + taskId.GetValue()),
TaskId: taskId,
SlaveId: offer.SlaveId,
Executor: sched.executor,
Resources: []*mesos.Resource{
util.NewScalarResource("cpus", CPUS_PER_TASK),
util.NewScalarResource("mem", MEM_PER_TASK),
},
}
log.Infof("Prepared task: %s with offer %s for launch\n", task.GetName(), offer.Id.GetValue())
tasks = append(tasks, task)
remainingCpus -= CPUS_PER_TASK
remainingMems -= MEM_PER_TASK
}
log.Infoln("Launching ", len(tasks), "tasks for offer", offer.Id.GetValue())
driver.LaunchTasks([]*mesos.OfferID{offer.Id}, tasks, &mesos.Filters{RefuseSeconds: proto.Float64(1)})
}
}
示例2: ResourceOffers
func (sched *ReflexScheduler) ResourceOffers(driver sched.SchedulerDriver, offers []*mesos.Offer) {
for _, offer := range offers {
// CPUs
cpuResources := util.FilterResources(
offer.Resources,
func(res *mesos.Resource) bool {
return res.GetName() == "cpus"
},
)
cpus := 0.0
for _, res := range cpuResources {
cpus += res.GetScalar().GetValue()
}
// Mem
memResources := util.FilterResources(
offer.Resources,
func(res *mesos.Resource) bool {
return res.GetName() == "mem"
},
)
mem := 0.0
for _, res := range memResources {
mem += res.GetScalar().GetValue()
}
logrus.WithFields(logrus.Fields{
"cpus": cpus,
"mem": mem,
}).Debug("got offer")
for _, pair := range sched.waitingPairs {
if pair.InProgress {
continue
}
task := pair.Task
if cpus >= task.CPU && mem >= task.Mem {
logrus.WithField("task", task).Info("would have launched a task")
}
}
driver.DeclineOffer(offer.GetId(), new(mesos.Filters))
}
}
示例3: resourcesHaveVolume
func resourcesHaveVolume(resources []*mesos.Resource, persistenceId string) bool {
filtered := util.FilterResources(resources, func(res *mesos.Resource) bool {
return res.GetName() == "disk" &&
res.Reservation != nil &&
res.Disk != nil &&
res.Disk.Persistence.GetId() == persistenceId
})
return len(filtered) > 0
}
示例4: ResourceOffers
func (sched *Scheduler) ResourceOffers(driver sched.SchedulerDriver, offers []*mesos.Offer) {
for _, offer := range offers {
taskId := &mesos.TaskID{
Value: proto.String(fmt.Sprintf("basicdocker-task-%d", time.Now().Unix())),
}
ports := util.FilterResources(
offer.Resources,
func(res *mesos.Resource) bool {
return res.GetName() == "ports"
},
)
if len(ports) > 0 && len(ports[0].GetRanges().GetRange()) > 0 {
} else {
return
}
task := &mesos.TaskInfo{
Name: proto.String(taskId.GetValue()),
TaskId: taskId,
SlaveId: offer.SlaveId,
Container: &mesos.ContainerInfo{
Type: mesos.ContainerInfo_DOCKER.Enum(),
Volumes: nil,
Hostname: nil,
Docker: &mesos.ContainerInfo_DockerInfo{
Image: &DOCKER_IMAGE_DEFAULT,
Network: mesos.ContainerInfo_DockerInfo_BRIDGE.Enum(),
},
},
Command: &mesos.CommandInfo{
Shell: proto.Bool(true),
Value: proto.String("set -x ; /bin/date ; /bin/hostname ; sleep 200 ; echo done"),
},
Executor: nil,
Resources: []*mesos.Resource{
util.NewScalarResource("cpus", getOfferCpu(offer)),
util.NewScalarResource("mem", getOfferMem(offer)),
util.NewRangesResource("ports", []*mesos.Value_Range{
util.NewValueRange(
*ports[0].GetRanges().GetRange()[0].Begin,
*ports[0].GetRanges().GetRange()[0].Begin+1,
),
}),
},
}
log.Infof("Prepared task: %s with offer %s for launch\n", task.GetName(), offer.Id.GetValue())
var tasks []*mesos.TaskInfo = []*mesos.TaskInfo{task}
log.Infoln("Launching ", len(tasks), " tasks for offer", offer.Id.GetValue())
driver.LaunchTasks([]*mesos.OfferID{offer.Id}, tasks, &mesos.Filters{RefuseSeconds: proto.Float64(1)})
sched.tasksLaunched++
time.Sleep(time.Second)
}
}
示例5: getScalarResources
func getScalarResources(offer *mesos.Offer, resourceName string) float64 {
resources := 0.0
filteredResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == resourceName
})
for _, res := range filteredResources {
resources += res.GetScalar().GetValue()
}
return resources
}
示例6: getPersistenceIds
func getPersistenceIds(resources []*mesos.Resource) []string {
filtered := util.FilterResources(resources, func(res *mesos.Resource) bool {
return res.GetName() == "disk" && res.Reservation != nil && res.Disk != nil
})
val := []string{}
for _, res := range filtered {
val = append(val, res.Disk.Persistence.GetId())
}
return val
}
示例7: FilterReservedVolumes
func FilterReservedVolumes(immutableResources []*mesos.Resource) []*mesos.Resource {
return util.FilterResources(immutableResources, func(res *mesos.Resource) bool {
if res.Reservation != nil &&
res.Disk != nil &&
res.GetName() == "disk" {
return true
}
return false
})
}
示例8: ScalarResourceVal
func ScalarResourceVal(name string, resources []*mesos.Resource) float64 {
scalarResources := util.FilterResources(resources, func(res *mesos.Resource) bool {
return res.GetType() == mesos.Value_SCALAR && res.GetName() == name
})
sum := 0.0
for _, res := range scalarResources {
sum += res.GetScalar().GetValue()
}
return sum
}
示例9: getRangeResources
func getRangeResources(offer *mesos.Offer, resourceName string) []*mesos.Value_Range {
resources := make([]*mesos.Value_Range, 0)
filteredResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == resourceName
})
for _, res := range filteredResources {
resources = append(resources, res.GetRanges().GetRange()...)
}
return resources
}
示例10: portIter
func portIter(resources []*mesos.Resource) chan int64 {
ports := make(chan int64)
go func() {
defer close(ports)
for _, resource := range util.FilterResources(resources, func(res *mesos.Resource) bool { return res.GetName() == "ports" }) {
for _, port := range common.RangesToArray(resource.GetRanges().GetRange()) {
ports <- port
}
}
}()
return ports
}
示例11: getOfferScalar
func getOfferScalar(offer *mesos.Offer, name string) float64 {
resources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == name
})
value := 0.0
for _, res := range resources {
value += res.GetScalar().GetValue()
}
return value
}
示例12: TestBadCPUAsk
func TestBadCPUAsk(t *testing.T) {
assert := assert.New(t)
offer2 := generateResourceOffer()
offer := generateResourceOffer()
askFun := AskForCPU(3.1)
newOffer, _, success := askFun(offer)
assert.Equal(false, success)
assert.Equal(offer, offer2)
cpuResources := util.FilterResources(newOffer, func(res *mesos.Resource) bool {
return res.GetName() == "cpus"
})
assert.Equal(3.0, cpuResources[0].Scalar.GetValue())
}
示例13: getPorts
func getPorts(resources []*mesos.Resource, withReservation bool) []int64 {
filtered := util.FilterResources(resources, func(res *mesos.Resource) bool {
if withReservation {
return res.GetName() == "ports" && res.Reservation != nil
}
return res.GetName() == "ports" && res.Reservation == nil
})
val := []int64{}
for _, res := range filtered {
val = append(val, RangesToArray(res.GetRanges().GetRange())...)
}
return val
}
示例14: getResource
func getResource(name string, resources []*mesos.Resource, withReservation bool) float64 {
filtered := util.FilterResources(resources, func(res *mesos.Resource) bool {
if withReservation {
return res.GetName() == name && res.Reservation != nil
}
return res.GetName() == name && res.Reservation == nil
})
val := 0.0
for _, res := range filtered {
val += res.GetScalar().GetValue()
}
return val
}
示例15: TestGoodPortAsk
func TestGoodPortAsk(t *testing.T) {
rand.Seed(10)
assert := assert.New(t)
offer := generateResourceOffer()
askFun := AskForPorts(100)
remaining, resourceAsk, success := askFun(offer)
assert.Equal(true, success)
assert.Equal(util.NewRangesResource("ports", []*mesos.Value_Range{util.NewValueRange(31105, 31204)}), resourceAsk)
remainingPorts := util.FilterResources(remaining, func(res *mesos.Resource) bool {
return res.GetName() == "ports"
})
assert.Equal([]*mesos.Resource{util.NewRangesResource("ports", []*mesos.Value_Range{util.NewValueRange(31000, 31104), util.NewValueRange(31205, 32000)})}, remainingPorts)
}