Search in sources :

Example 1 with ComputerDriverException

use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.

the class KubernetesDriverTest method testUploadAlgorithmJarWithError.

@Test
public void testUploadAlgorithmJarWithError() throws FileNotFoundException {
    Whitebox.setInternalState(this.driver, "bashPath", "conf/images/upload_test-x.sh");
    InputStream inputStream = new FileInputStream("conf/images/test.jar");
    Assert.assertThrows(ComputerDriverException.class, () -> {
        this.driver.uploadAlgorithmJar("PageRank", inputStream);
    }, e -> {
        ComputerDriverException exception = (ComputerDriverException) e;
        Assert.assertContains("No such file", exception.rootCause().getMessage());
    });
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ComputerDriverException(com.baidu.hugegraph.computer.driver.ComputerDriverException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with ComputerDriverException

use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.

the class KubernetesDriver method createKubeClient.

private static NamespacedKubernetesClient createKubeClient(HugeConfig conf) {
    String kubeConfig = conf.get(KubeDriverOptions.KUBE_CONFIG);
    Config config;
    try {
        File file = new File(kubeConfig);
        @SuppressWarnings("deprecation") String kubeConfigContents = FileUtils.readFileToString(file);
        config = Config.fromKubeconfig(kubeConfigContents);
    } catch (IOException e) {
        throw new ComputerDriverException("Failed to read KubeConfig: %s", e, kubeConfig);
    }
    return new DefaultKubernetesClient(config);
}
Also used : Config(io.fabric8.kubernetes.client.Config) HugeConfig(com.baidu.hugegraph.config.HugeConfig) ComputerDriverException(com.baidu.hugegraph.computer.driver.ComputerDriverException) IOException(java.io.IOException) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File)

Example 3 with ComputerDriverException

use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.

the class KubernetesDriver method defaultSpec.

private Map<String, Object> defaultSpec() {
    Map<String, Object> defaultSpec = new HashMap<>();
    Collection<TypedOption<?, ?>> options = KubeSpecOptions.instance().options().values();
    for (TypedOption<?, ?> typeOption : options) {
        Object value = this.conf.get(typeOption);
        if (value != null) {
            String specKey = KubeUtil.covertSpecKey(typeOption.name());
            if (KubeSpecOptions.MAP_TYPE_CONFIGS.contains(typeOption)) {
                if (!Objects.equals(String.valueOf(value), "[]")) {
                    value = this.conf.getMap((ConfigListOption<String>) typeOption);
                    defaultSpec.put(specKey, value);
                }
            } else {
                defaultSpec.put(specKey, value);
            }
        }
    }
    ComputerJobSpec spec = HugeGraphComputerJob.mapToSpec(defaultSpec);
    // Add pullSecrets
    List<String> secretNames = this.conf.get(KubeDriverOptions.PULL_SECRET_NAMES);
    if (CollectionUtils.isNotEmpty(secretNames)) {
        List<LocalObjectReference> secrets = new ArrayList<>();
        for (String name : secretNames) {
            if (StringUtils.isBlank(name)) {
                continue;
            }
            secrets.add(new LocalObjectReference(name));
        }
        if (CollectionUtils.isNotEmpty(secrets)) {
            spec.withPullSecrets(secrets);
        }
    }
    // Add log4j.xml
    String log4jXmlPath = this.conf.get(KubeDriverOptions.LOG4J_XML_PATH);
    if (StringUtils.isNotBlank(log4jXmlPath)) {
        try {
            File file = new File(log4jXmlPath);
            @SuppressWarnings("deprecation") String log4jXml = FileUtils.readFileToString(file);
            spec.withLog4jXml(log4jXml);
        } catch (IOException exception) {
            throw new ComputerDriverException("Failed to read log4j file for computer job", exception);
        }
    }
    Map<String, Object> specMap = HugeGraphComputerJob.specToMap(spec);
    return Collections.unmodifiableMap(specMap);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ConfigListOption(com.baidu.hugegraph.config.ConfigListOption) ComputerJobSpec(com.baidu.hugegraph.computer.k8s.crd.model.ComputerJobSpec) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) ComputerDriverException(com.baidu.hugegraph.computer.driver.ComputerDriverException) TypedOption(com.baidu.hugegraph.config.TypedOption) File(java.io.File)

Example 4 with ComputerDriverException

use of com.baidu.hugegraph.computer.driver.ComputerDriverException in project hugegraph-computer by hugegraph.

the class KubernetesDriver method uploadAlgorithmJar.

@Override
public void uploadAlgorithmJar(String algorithmName, InputStream input) {
    File tempFile = null;
    try {
        Path path = Files.createDirectories(Paths.get(TMP_DIR, UUID.randomUUID().toString()));
        tempFile = File.createTempFile("userAlgorithm", ".jar", path.toFile());
        FileUtils.copyInputStreamToFile(input, tempFile);
        InputStream bashStream;
        if (StringUtils.isBlank(this.bashPath)) {
            bashStream = this.getClass().getResourceAsStream(DEFAULT_PUSH_BASH_PATH);
        } else {
            bashStream = new FileInputStream(this.bashPath);
        }
        String bashAsStr = IOHelpers.readFully(bashStream);
        StringBuilder builder = new StringBuilder();
        builder.append(BUILD_IMAGE_FUNC);
        if (StringUtils.isNotBlank(this.registry)) {
            builder.append(" -r ").append(this.registry);
        }
        if (StringUtils.isNotBlank(this.username)) {
            builder.append(" -u ").append(this.username);
        }
        if (StringUtils.isNotBlank(this.password)) {
            builder.append(" -p ").append(this.password);
        }
        builder.append(" -s ").append(tempFile.getAbsolutePath());
        String jarFile = this.buildJarFile(this.jarFileDir, algorithmName);
        builder.append(" -j ").append(jarFile);
        String imageUrl = this.buildImageUrl(algorithmName);
        builder.append(" -i ").append(imageUrl);
        builder.append(" -f ").append(this.frameworkImageUrl);
        String args = builder.toString();
        String[] command = { "bash", "-c", bashAsStr + "\n" + args };
        Process process = Runtime.getRuntime().exec(command);
        int code = process.waitFor();
        if (code != 0) {
            InputStream errorStream = process.getErrorStream();
            String errorInfo = IOHelpers.readFully(errorStream);
            if (StringUtils.isBlank(errorInfo)) {
                InputStream stdoutStream = process.getInputStream();
                errorInfo = IOHelpers.readFully(stdoutStream);
            }
            throw new ComputerDriverException(errorInfo);
        }
    } catch (Throwable exception) {
        throw new ComputerDriverException("Failed to upload algorithm Jar", exception);
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
}
Also used : Path(java.nio.file.Path) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ComputerDriverException(com.baidu.hugegraph.computer.driver.ComputerDriverException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ComputerDriverException (com.baidu.hugegraph.computer.driver.ComputerDriverException)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ComputerJobSpec (com.baidu.hugegraph.computer.k8s.crd.model.ComputerJobSpec)1 ConfigListOption (com.baidu.hugegraph.config.ConfigListOption)1 HugeConfig (com.baidu.hugegraph.config.HugeConfig)1 TypedOption (com.baidu.hugegraph.config.TypedOption)1 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)1 Config (io.fabric8.kubernetes.client.Config)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1