def test_get_max_container_count(test_db): # 4个16核, 不限制共享数 pod = _create_data(10, -1, 4) assert get_max_container_count(pod, ncore=1, nshare=0) == 64 assert get_max_container_count(pod, ncore=2, nshare=0) == 32 assert get_max_container_count(pod, ncore=3, nshare=0) == 20 assert get_max_container_count(pod, ncore=4, nshare=0) == 16 assert get_max_container_count(pod, ncore=5, nshare=0) == 12 assert get_max_container_count(pod, ncore=1, nshare=5) == 40 assert get_max_container_count(pod, ncore=2, nshare=5) == 24 assert get_max_container_count(pod, ncore=3, nshare=5) == 16 assert get_max_container_count(pod, ncore=1, nshare=1) == 56 assert get_max_container_count(pod, ncore=2, nshare=1) == 28
def test_get_max_container_count(test_db): # 4个16核, 不限制共享数 group, pod = _create_data(10, -1, 4) assert get_max_container_count(group, pod, ncore=1, nshare=0) == 64 assert get_max_container_count(group, pod, ncore=2, nshare=0) == 32 assert get_max_container_count(group, pod, ncore=3, nshare=0) == 20 assert get_max_container_count(group, pod, ncore=4, nshare=0) == 16 assert get_max_container_count(group, pod, ncore=5, nshare=0) == 12 assert get_max_container_count(group, pod, ncore=1, nshare=5) == 40 assert get_max_container_count(group, pod, ncore=2, nshare=5) == 24 assert get_max_container_count(group, pod, ncore=3, nshare=5) == 16 assert get_max_container_count(group, pod, ncore=1, nshare=1) == 56 assert get_max_container_count(group, pod, ncore=2, nshare=1) == 28
def test_group_pod(test_db): p1 = Pod.create('pod1', 'pod1') p2 = Pod.create('pod1', 'pod1') assert p1 is not None assert p1.name == 'pod1' assert p2 is None p3 = Pod.get_by_name('pod1') assert p3.id == p1.id assert p3.get_free_public_hosts(10) == [] assert get_max_container_count(p3, 1, 2) == 0 assert centralized_schedule(p3, 1, 1, 2) == {}
def test_get_max_container_count_single_host(test_db): pod = Pod.create('pod', 'pod', 10, -1) Host.create(pod, random_ipv4(), random_string(), random_uuid(), 64, 4096) assert get_max_container_count(pod, ncore=1, nshare=0) == 64 assert get_max_container_count(pod, ncore=2, nshare=0) == 32 assert get_max_container_count(pod, ncore=3, nshare=0) == 21 assert get_max_container_count(pod, ncore=4, nshare=0) == 16 assert get_max_container_count(pod, ncore=5, nshare=0) == 12 assert get_max_container_count(pod, ncore=1, nshare=5) == 42 assert get_max_container_count(pod, ncore=2, nshare=5) == 25
def group_max_containers(group_name): pod_name = request.args.get('pod_name', default='') core_require = request.args.get('ncore', type=float, default=1) group = Group.get_by_name(group_name) if not group: abort(400, 'No group found') pod = Pod.get_by_name(pod_name) if not pod: abort(400, 'No pod found') ncore, nshare = pod.get_core_allocation(core_require) return {'r':0, 'msg': consts.OK, 'data': get_max_container_count(group, pod, ncore, nshare)}
def test_get_max_container_count_single_host(test_db): group = Group.create('group', 'group') pod = Pod.create('pod', 'pod', 10, -1) host = Host.create(pod, random_ipv4(), random_string(), random_uuid(), 64, 4096) host.assigned_to_group(group) assert get_max_container_count(group, pod, ncore=1, nshare=0) == 64 assert get_max_container_count(group, pod, ncore=2, nshare=0) == 32 assert get_max_container_count(group, pod, ncore=3, nshare=0) == 21 assert get_max_container_count(group, pod, ncore=4, nshare=0) == 16 assert get_max_container_count(group, pod, ncore=5, nshare=0) == 12 assert get_max_container_count(group, pod, ncore=1, nshare=5) == 42 assert get_max_container_count(group, pod, ncore=2, nshare=5) == 25
def group_max_containers(group_name): pod_name = request.args.get('pod_name', type=str, default='') core_require = request.args.get('ncore', type=float, default=1) group = Group.get_by_name(group_name) if not group: raise EruAbortException(consts.HTTP_BAD_REQUEST, 'No group found') pod = Pod.get_by_name(pod_name) if not pod: raise EruAbortException(consts.HTTP_BAD_REQUEST, 'No pod found') core_require = int(core_require * pod.core_share) # 是说一个容器要几个核... ncore = core_require / pod.core_share nshare = core_require % pod.core_share return {'r':0, 'msg': consts.OK, 'data': get_max_container_count(group, pod, ncore, nshare)}
def test_group_pod(test_db): g1 = Group.create('group1', 'group1') g2 = Group.create('group1', 'group1') assert g1 is not None assert g1.name == 'group1' assert g2 is None p1 = Pod.create('pod1', 'pod1') p2 = Pod.create('pod1', 'pod1') assert p1 is not None assert p1.name == 'pod1' assert p2 is None g3 = Group.get_by_name('group1') assert g3.id == g1.id p3 = Pod.get_by_name('pod1') assert p3.id == p1.id assert p3.assigned_to_group(g3) assert p3.get_free_public_hosts(10) == [] assert get_max_container_count(g3, p3, 1, 2) == 0 assert centralized_schedule(g3, p3, 1, 1, 2) == {}
def test_host(test_db): p = Pod.create('pod', 'pod', 10, -1) hosts = [ Host.create(p, random_ipv4(), random_string(prefix='host'), random_uuid(), 4, 4096) for i in range(6) ] for host in hosts: host.set_public() assert host is not None assert len(host.cores) == 4 full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(part_cores) == 0 assert len(p.get_private_hosts()) == 0 assert get_max_container_count(p, 1, 0) == 0 assert centralized_schedule(p, 1, 1, 0) == {} for host in hosts[:3]: host.set_private() host_ids1 = {h.id for h in hosts[:3]} host_ids2 = {h.id for h in hosts[3:]} assert get_max_container_count(p, 1, 0) == 12 host_cores = centralized_schedule(p, 12, 1, 0) assert len(host_cores) == 3 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 4 assert len(cores['full']) == 4 assert get_max_container_count(p, 3, 0) == 3 host_cores = centralized_schedule(p, 3, 3, 0) assert len(host_cores) == 3 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 1 assert len(cores['full']) == 3 assert get_max_container_count(p, 2, 0) == 6 host_cores = centralized_schedule(p, 4, 2, 0) assert len(host_cores) == 2 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 2 assert len(cores['full']) == 4 assert get_max_container_count(p, 1, 1) == 9 host_cores = centralized_schedule(p, 3, 1, 1) assert len(host_cores) == 1 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 3 assert len(cores['full']) == 3 assert len(cores['part']) == 3 assert get_max_container_count(p, 2, 3) == 3 host_cores = centralized_schedule(p, 3, 2, 3) assert len(host_cores) == 3 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 1 assert len(cores['full']) == 2 assert len(cores['part']) == 1
def test_container(test_db): a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git') assert a is not None assert a.id == a.user_id v = a.add_version(random_sha1()) assert v is not None assert v.app.id == a.id assert v.name == a.name assert len(v.containers.all()) == 0 assert len(v.tasks.all()) == 0 p = Pod.create('pod', 'pod', 10, -1) hosts = [ Host.create(p, random_ipv4(), random_string(prefix='host'), random_uuid(), 4, 4096) for i in range(6) ] for host in hosts[3:]: host.set_public() host_ids1 = {h.id for h in hosts[:3]} host_ids2 = {h.id for h in hosts[3:]} host_cores = centralized_schedule(p, 3, 3, 0) #测试没有碎片核的情况 #获取核 containers = [] for (host, count), cores in host_cores.iteritems(): host.occupy_cores(cores, 0) cores_per_container = len(cores['full']) / count for i in range(count): cid = random_sha1() used_cores = { 'full': cores['full'][i * cores_per_container:(i + 1) * cores_per_container] } c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env', nshare=0) assert c is not None containers.append(c) for host in p.get_private_hosts(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 1 assert len(part_cores) == 0 assert len(host.containers.all()) == 1 assert host.count == 1 assert len(containers) == 3 assert len(v.containers.all()) == 3 for c in containers: assert c.host_id in host_ids1 assert c.host_id not in host_ids2 assert c.app.id == a.id assert c.version.id == v.id assert c.is_alive assert len(c.full_cores) == 3 assert len(c.part_cores) == 0 all_core_labels = sorted([ '0', '1', '2', '3', ]) used_full_core_labels = [core.label for core in c.full_cores] used_part_core_labels = [core.label for core in c.part_cores] free_core_labels = [core.label for core in c.host.get_free_cores()[0]] assert all_core_labels == sorted(used_full_core_labels + used_part_core_labels + free_core_labels) #释放核 for c in containers: c.delete() assert len(v.containers.all()) == 0 assert get_max_container_count(p, 3, 0) == 3 host_cores = centralized_schedule(p, 3, 3, 0) assert len(host_cores) == 3 for host in p.get_private_hosts(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(part_cores) == 0 assert len(host.containers.all()) == 0 assert host.count == 4 #测试有碎片的情况 #获取核 host_cores = centralized_schedule(p, 3, 3, 4) containers = [] for (host, count), cores in host_cores.iteritems(): cores_per_container = len(cores['full']) / count host.occupy_cores(cores, 4) for i in range(count): cid = random_sha1() used_cores = { 'full': cores['full'][i * cores_per_container:(i + 1) * cores_per_container], 'part': cores['part'] } # not using a port c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env', nshare=4) assert c is not None containers.append(c) for host in p.get_private_hosts(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 0 assert len(part_cores) == 1 assert part_cores[0].remain == 6 assert len(host.containers.all()) == 1 assert host.count == D('0.6') assert len(containers) == 3 assert len(v.containers.all()) == 3 for c in containers: assert c.host_id in host_ids1 assert c.host_id not in host_ids2 assert c.app.id == a.id assert c.version.id == v.id assert c.is_alive assert len(c.full_cores) == 3 assert len(c.part_cores) == 1 all_core_labels = sorted([ '0', '1', '2', '3', ]) used_full_core_labels = [core.label for core in c.full_cores] used_part_core_labels = [core.label for core in c.part_cores] free_core_labels = [core.label for core in c.host.get_free_cores()[0]] assert all_core_labels == sorted(used_full_core_labels + used_part_core_labels + free_core_labels) #释放核 for c in containers: c.delete() assert len(v.containers.all()) == 0 assert get_max_container_count(p, 3, 0) == 3 host_cores = centralized_schedule(p, 3, 3, 0) assert len(host_cores) == 3 for host in p.get_private_hosts(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(host.containers.all()) == 0 assert host.count == 4 #获取 host_cores = centralized_schedule(p, 6, 1, 5) containers = [] for (host, count), cores in host_cores.iteritems(): cores_per_container = len(cores['full']) / count for i in range(count): cid = random_sha1() used_cores = { 'full': cores['full'][i * cores_per_container:(i + 1) * cores_per_container], 'part': cores['part'][i:i + 1], } host.occupy_cores(used_cores, 5) # not using a port c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env', nshare=5) assert c is not None containers.append(c) for host in p.get_private_hosts(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 1 assert len(part_cores) == 0 assert len(host.containers.all()) == 2 assert host.count == D('1') assert len(containers) == 6 assert len(v.containers.all()) == 6 for c in containers: assert c.host_id in host_ids1 assert c.host_id not in host_ids2 assert c.app.id == a.id assert c.version.id == v.id assert c.is_alive assert len(c.full_cores) == 1 assert len(c.part_cores) == 1 ##释放核 for c in containers: c.delete() assert len(v.containers.all()) == 0 assert get_max_container_count(p, 3, 0) == 3 host_cores = centralized_schedule(p, 3, 3, 0) assert len(host_cores) == 3 for host in p.get_private_hosts(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(part_cores) == 0 assert len(host.containers.all()) == 0 assert host.count == 4
def test_host(test_db): g = Group.create('group', 'group') p = Pod.create('pod', 'pod', 10, -1) assert p.assigned_to_group(g) hosts = [Host.create(p, random_ipv4(), random_string(prefix='host'), random_uuid(), 4, 4096) for i in range(6)] for host in hosts: assert host is not None assert len(host.cores) == 4 full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(part_cores) == 0 assert len(g.private_hosts.all()) == 0 assert get_max_container_count(g, p, 1, 0) == 0 assert centralized_schedule(g, p, 1, 1, 0) == {} for host in hosts[:3]: host.assigned_to_group(g) host_ids1 = {h.id for h in hosts[:3]} host_ids2 = {h.id for h in hosts[3:]} assert len(g.private_hosts.all()) == 3 assert get_max_container_count(g, p, 1, 0) == 12 host_cores = centralized_schedule(g, p, 12, 1, 0) assert len(host_cores) == 3 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 4 assert len(cores['full']) == 4 assert get_max_container_count(g, p, 3, 0) == 3 host_cores = centralized_schedule(g, p, 3, 3, 0) assert len(host_cores) == 3 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 1 assert len(cores['full']) == 3 assert get_max_container_count(g, p, 2, 0) == 6 host_cores = centralized_schedule(g, p, 4, 2, 0) assert len(host_cores) == 2 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 2 assert len(cores['full']) == 4 assert get_max_container_count(g, p, 1, 1) == 9 host_cores = centralized_schedule(g, p, 3, 1, 1) assert len(host_cores) == 1 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 3 assert len(cores['full']) == 3 assert len(cores['part']) == 3 assert get_max_container_count(g, p, 2, 3) == 3 host_cores = centralized_schedule(g, p, 3, 2, 3) assert len(host_cores) == 3 for (host, count), cores in host_cores.iteritems(): assert host.id in host_ids1 assert host.id not in host_ids2 assert count == 1 assert len(cores['full']) == 2 assert len(cores['part']) == 1
def test_container(test_db): a = App.get_or_create('app', 'http://git.hunantv.com/group/app.git', '') assert a is not None assert a.id == a.user_id v = a.add_version(random_sha1()) assert v is not None assert v.app.id == a.id assert v.name == a.name assert len(v.containers.all()) == 0 assert len(v.tasks.all()) == 0 g = Group.create('group', 'group') p = Pod.create('pod', 'pod', 10, -1) assert p.assigned_to_group(g) hosts = [Host.create(p, random_ipv4(), random_string(prefix='host'), random_uuid(), 4, 4096) for i in range(6)] for host in hosts[:3]: host.assigned_to_group(g) host_ids1 = {h.id for h in hosts[:3]} host_ids2 = {h.id for h in hosts[3:]} host_cores = centralized_schedule(g, p, 3, 3, 0) #测试没有碎片核的情况 #获取核 containers = [] for (host, count), cores in host_cores.iteritems(): host.occupy_cores(cores, 0) cores_per_container = len(cores['full']) / count for i in range(count): cid = random_sha1() used_cores = {'full': cores['full'][i*cores_per_container:(i+1)*cores_per_container]} c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env', nshare=0) assert c is not None containers.append(c) for host in g.private_hosts.all(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 1 assert len(part_cores) == 0 assert len(host.containers.all()) == 1 assert host.count == 1 assert len(containers) == 3 assert len(v.containers.all()) == 3 for c in containers: assert c.host_id in host_ids1 assert c.host_id not in host_ids2 assert c.app.id == a.id assert c.version.id == v.id assert c.is_alive assert len(c.full_cores) == 3 assert len(c.part_cores) == 0 all_core_labels = sorted(['0', '1', '2', '3', ]) used_full_core_labels = [core.label for core in c.full_cores] used_part_core_labels = [core.label for core in c.part_cores] free_core_labels = [core.label for core in c.host.get_free_cores()[0]] assert all_core_labels == sorted(used_full_core_labels + used_part_core_labels + free_core_labels) #释放核 for c in containers: c.delete() assert len(v.containers.all()) == 0 assert get_max_container_count(g, p, 3, 0) == 3 host_cores = centralized_schedule(g, p, 3, 3, 0) assert len(host_cores) == 3 for host in g.private_hosts.all(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(part_cores) == 0 assert len(host.containers.all()) == 0 assert host.count == 4 #测试有碎片的情况 #获取核 host_cores = centralized_schedule(g, p, 3, 3, 4) containers = [] for (host, count), cores in host_cores.iteritems(): cores_per_container = len(cores['full']) / count host.occupy_cores(cores, 4) for i in range(count): cid = random_sha1() used_cores = {'full': cores['full'][i*cores_per_container:(i+1)*cores_per_container], 'part': cores['part']} # not using a port c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env', nshare=4) assert c is not None containers.append(c) for host in g.private_hosts.all(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 0 assert len(part_cores) == 1 assert part_cores[0].remain == 6 assert len(host.containers.all()) == 1 assert host.count == D('0.6') assert len(containers) == 3 assert len(v.containers.all()) == 3 for c in containers: assert c.host_id in host_ids1 assert c.host_id not in host_ids2 assert c.app.id == a.id assert c.version.id == v.id assert c.is_alive assert len(c.full_cores) == 3 assert len(c.part_cores) == 1 all_core_labels = sorted(['0', '1', '2', '3', ]) used_full_core_labels = [core.label for core in c.full_cores] used_part_core_labels = [core.label for core in c.part_cores] free_core_labels = [core.label for core in c.host.get_free_cores()[0]] assert all_core_labels == sorted(used_full_core_labels + used_part_core_labels + free_core_labels) #释放核 for c in containers: c.delete() assert len(v.containers.all()) == 0 assert get_max_container_count(g, p, 3, 0) == 3 host_cores = centralized_schedule(g, p, 3, 3, 0) assert len(host_cores) == 3 for host in g.private_hosts.all(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(host.containers.all()) == 0 assert host.count == 4 #获取 host_cores = centralized_schedule(g, p, 6, 1, 5) containers = [] for (host, count), cores in host_cores.iteritems(): cores_per_container = len(cores['full']) / count for i in range(count): cid = random_sha1() used_cores = { 'full': cores['full'][i*cores_per_container:(i+1)*cores_per_container], 'part': cores['part'][i:i+1], } host.occupy_cores(used_cores, 5) # not using a port c = Container.create(cid, host, v, random_string(), 'entrypoint', used_cores, 'env', nshare=5) assert c is not None containers.append(c) for host in g.private_hosts.all(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 1 assert len(part_cores) == 0 assert len(host.containers.all()) == 2 assert host.count == D('1') assert len(containers) == 6 assert len(v.containers.all()) == 6 for c in containers: assert c.host_id in host_ids1 assert c.host_id not in host_ids2 assert c.app.id == a.id assert c.version.id == v.id assert c.is_alive assert len(c.full_cores) == 1 assert len(c.part_cores) == 1 ##释放核 for c in containers: c.delete() assert len(v.containers.all()) == 0 assert get_max_container_count(g, p, 3, 0) == 3 host_cores = centralized_schedule(g, p, 3, 3, 0) assert len(host_cores) == 3 for host in g.private_hosts.all(): full_cores, part_cores = host.get_free_cores() assert len(full_cores) == 4 assert len(part_cores) == 0 assert len(host.containers.all()) == 0 assert host.count == 4
def test_max_container_count(ncore, nshare, expected): start = time.time() assert get_max_container_count(pod, ncore, nshare) == expected delta = time.time() - start _log.debug('test_max_container_count with ncore={}, nshare={}, expected={} takes {}'.format(ncore, nshare, expected, delta))