Search in sources :

Example 1 with ModuleConflictException

use of io.cdap.cdap.data2.dataset2.ModuleConflictException in project cdap by caskdata.

the class DatasetModulesDeployer method loadAndDeployModule.

private void loadAndDeployModule(ClassLoader artifactClassLoader, String className, final Location jarLocation, String moduleName, NamespaceId namespaceId, String authorizingUser) throws Exception {
    // note: using app class loader to load module class
    @SuppressWarnings("unchecked") Class<Dataset> clazz = (Class<Dataset>) artifactClassLoader.loadClass(className);
    try {
        // note: we can deploy module or create module from Dataset class
        // note: it seems dangerous to instantiate dataset module here, but this will be fine when we move deploy into
        // isolated user's environment (e.g. separate yarn container)
        final DatasetModuleId moduleId = namespaceId.datasetModule(moduleName);
        final DatasetModule module;
        if (DatasetModule.class.isAssignableFrom(clazz)) {
            module = (DatasetModule) clazz.newInstance();
        } else if (Dataset.class.isAssignableFrom(clazz)) {
            if (systemDatasetFramework.hasSystemType(clazz.getName())) {
                return;
            }
            final DatasetTypeId typeId = namespaceId.datasetType(clazz.getName());
            boolean hasType = AuthorizationUtil.authorizeAs(authorizingUser, new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    return datasetFramework.hasType(typeId);
                }
            });
            if (hasType && !allowDatasetUncheckedUpgrade) {
                return;
            }
            module = new SingleTypeModule(clazz);
        } else {
            throw new IllegalArgumentException(String.format("Cannot use class %s to add dataset module: it must be of type DatasetModule or Dataset", clazz.getName()));
        }
        LOG.info("Adding module: {}", clazz.getName());
        AuthorizationUtil.authorizeAs(authorizingUser, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                datasetFramework.addModule(moduleId, module, jarLocation);
                return null;
            }
        });
    } catch (ModuleConflictException e) {
        LOG.info("Conflict while deploying module {}: {}", moduleName, e.getMessage());
        throw e;
    }
}
Also used : DatasetTypeId(co.cask.cdap.proto.id.DatasetTypeId) Dataset(co.cask.cdap.api.dataset.Dataset) DatasetModule(co.cask.cdap.api.dataset.module.DatasetModule) Callable(java.util.concurrent.Callable) ModuleConflictException(co.cask.cdap.data2.dataset2.ModuleConflictException) DatasetModuleId(co.cask.cdap.proto.id.DatasetModuleId) ModuleConflictException(co.cask.cdap.data2.dataset2.ModuleConflictException) SingleTypeModule(co.cask.cdap.data2.dataset2.SingleTypeModule)

Example 2 with ModuleConflictException

use of io.cdap.cdap.data2.dataset2.ModuleConflictException in project cdap by caskdata.

the class DatasetModulesDeployer method loadAndDeployModule.

private void loadAndDeployModule(ClassLoader artifactClassLoader, String className, final Location jarLocation, String moduleName, NamespaceId namespaceId, String authorizingUser) throws Exception {
    // note: using app class loader to load module class
    @SuppressWarnings("unchecked") Class<Dataset> clazz = (Class<Dataset>) artifactClassLoader.loadClass(className);
    try {
        // note: we can deploy module or create module from Dataset class
        // note: it seems dangerous to instantiate dataset module here, but this will be fine when we move deploy into
        // isolated user's environment (e.g. separate yarn container)
        final DatasetModuleId moduleId = namespaceId.datasetModule(moduleName);
        final DatasetModule module;
        if (DatasetModule.class.isAssignableFrom(clazz)) {
            module = (DatasetModule) clazz.newInstance();
        } else if (Dataset.class.isAssignableFrom(clazz)) {
            if (systemDatasetFramework.hasSystemType(clazz.getName())) {
                return;
            }
            final DatasetTypeId typeId = namespaceId.datasetType(clazz.getName());
            boolean hasType = AuthorizationUtil.authorizeAs(authorizingUser, new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    return datasetFramework.hasType(typeId);
                }
            });
            if (hasType && !allowDatasetUncheckedUpgrade) {
                return;
            }
            module = new SingleTypeModule(clazz);
        } else {
            throw new IllegalArgumentException(String.format("Cannot use class %s to add dataset module: it must be of type DatasetModule or Dataset", clazz.getName()));
        }
        LOG.info("Adding module: {}", clazz.getName());
        AuthorizationUtil.authorizeAs(authorizingUser, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                datasetFramework.addModule(moduleId, module, jarLocation);
                return null;
            }
        });
    } catch (ModuleConflictException e) {
        LOG.info("Conflict while deploying module {}: {}", moduleName, e.getMessage());
        throw e;
    }
}
Also used : DatasetTypeId(io.cdap.cdap.proto.id.DatasetTypeId) Dataset(io.cdap.cdap.api.dataset.Dataset) DatasetModule(io.cdap.cdap.api.dataset.module.DatasetModule) Callable(java.util.concurrent.Callable) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) ModuleConflictException(io.cdap.cdap.data2.dataset2.ModuleConflictException) DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) ModuleConflictException(io.cdap.cdap.data2.dataset2.ModuleConflictException) SingleTypeModule(io.cdap.cdap.data2.dataset2.SingleTypeModule)

Example 3 with ModuleConflictException

use of io.cdap.cdap.data2.dataset2.ModuleConflictException in project cdap by caskdata.

the class DatasetServiceClient method addModule.

public void addModule(String moduleName, String className, Location jarLocation) throws DatasetManagementException {
    HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.PUT, "modules/" + moduleName).addHeader("X-Class-Name", className).withBody(Locations.newInputSupplier(jarLocation));
    HttpResponse response = doRequest(requestBuilder);
    if (HttpResponseStatus.CONFLICT.code() == response.getResponseCode()) {
        throw new ModuleConflictException(String.format("Failed to add module %s due to conflict, details: %s", moduleName, response));
    }
    if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Failed to add module %s, details: %s", moduleName, response));
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) ModuleConflictException(co.cask.cdap.data2.dataset2.ModuleConflictException) HttpResponse(co.cask.common.http.HttpResponse)

Example 4 with ModuleConflictException

use of io.cdap.cdap.data2.dataset2.ModuleConflictException in project cdap by caskdata.

the class DatasetServiceClient method addModule.

public void addModule(String moduleName, String className, Location jarLocation) throws DatasetManagementException, UnauthorizedException {
    HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.PUT, "modules/" + moduleName).addHeader("X-Class-Name", className).withBody((ContentProvider<? extends InputStream>) jarLocation::getInputStream);
    HttpResponse response = doRequest(requestBuilder);
    if (HttpResponseStatus.CONFLICT.code() == response.getResponseCode()) {
        throw new ModuleConflictException(String.format("Failed to add module %s due to conflict, details: %s", moduleName, response));
    }
    if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Failed to add module %s, details: %s", moduleName, response));
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) ModuleConflictException(io.cdap.cdap.data2.dataset2.ModuleConflictException) HttpResponse(io.cdap.common.http.HttpResponse)

Aggregations

ModuleConflictException (co.cask.cdap.data2.dataset2.ModuleConflictException)2 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)2 ModuleConflictException (io.cdap.cdap.data2.dataset2.ModuleConflictException)2 Callable (java.util.concurrent.Callable)2 Dataset (co.cask.cdap.api.dataset.Dataset)1 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)1 DatasetModule (co.cask.cdap.api.dataset.module.DatasetModule)1 SingleTypeModule (co.cask.cdap.data2.dataset2.SingleTypeModule)1 DatasetModuleId (co.cask.cdap.proto.id.DatasetModuleId)1 DatasetTypeId (co.cask.cdap.proto.id.DatasetTypeId)1 HttpRequest (co.cask.common.http.HttpRequest)1 HttpResponse (co.cask.common.http.HttpResponse)1 Dataset (io.cdap.cdap.api.dataset.Dataset)1 DatasetModule (io.cdap.cdap.api.dataset.module.DatasetModule)1 SingleTypeModule (io.cdap.cdap.data2.dataset2.SingleTypeModule)1 DatasetModuleId (io.cdap.cdap.proto.id.DatasetModuleId)1 DatasetTypeId (io.cdap.cdap.proto.id.DatasetTypeId)1 HttpRequest (io.cdap.common.http.HttpRequest)1 HttpResponse (io.cdap.common.http.HttpResponse)1