Search in sources :

Example 1 with Storable

use of com.hortonworks.registries.storage.Storable in project streamline by hortonworks.

the class TestApplication method getCacheBackedDao.

private StorageManager getCacheBackedDao(TestConfiguration testConfiguration) {
    StorageProviderConfiguration storageProviderConfiguration = testConfiguration.getStorageProviderConfiguration();
    final StorageManager dao = getStorageManager(storageProviderConfiguration);
    final CacheBuilder cacheBuilder = getGuavaCacheBuilder();
    final Cache<StorableKey, Storable> cache = getCache(dao, cacheBuilder);
    final StorageWriter storageWriter = getStorageWriter(dao);
    return doGetCacheBackedDao(cache, storageWriter);
}
Also used : CacheBuilder(com.google.common.cache.CacheBuilder) StorableKey(com.hortonworks.registries.storage.StorableKey) CacheBackedStorageManager(com.hortonworks.registries.storage.CacheBackedStorageManager) StorageManager(com.hortonworks.registries.storage.StorageManager) Storable(com.hortonworks.registries.storage.Storable) StorageWriter(com.hortonworks.registries.storage.cache.writer.StorageWriter)

Example 2 with Storable

use of com.hortonworks.registries.storage.Storable in project streamline by hortonworks.

the class StreamlineApplication method registerResources.

private void registerResources(StreamlineConfiguration configuration, Environment environment, Subject subject) throws ConfigException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    StorageManager storageManager = getDao(configuration);
    TransactionManager transactionManager;
    if (storageManager instanceof TransactionManager) {
        transactionManager = (TransactionManager) storageManager;
    } else {
        transactionManager = new NOOPTransactionManager();
    }
    environment.jersey().register(new TransactionEventListener(transactionManager, true));
    Collection<Class<? extends Storable>> streamlineEntities = getStorableEntities();
    storageManager.registerStorables(streamlineEntities);
    LOG.info("Registered streamline entities {}", streamlineEntities);
    FileStorage fileStorage = this.getJarStorage(configuration, storageManager);
    int appPort = ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getApplicationConnectors().get(0)).getPort();
    String catalogRootUrl = configuration.getCatalogRootUrl().replaceFirst("8080", appPort + "");
    List<ModuleConfiguration> modules = configuration.getModules();
    List<Object> resourcesToRegister = new ArrayList<>();
    // add StreamlineConfigResource
    resourcesToRegister.add(new StreamlineConfigurationResource(configuration));
    // authorizer
    StreamlineAuthorizer authorizer;
    AuthorizerConfiguration authorizerConf = configuration.getAuthorizerConfiguration();
    SecurityCatalogService securityCatalogService = new SecurityCatalogService(storageManager);
    if (authorizerConf != null) {
        authorizer = ((Class<StreamlineAuthorizer>) Class.forName(authorizerConf.getClassName())).newInstance();
        Map<String, Object> authorizerConfig = new HashMap<>();
        authorizerConfig.put(DefaultStreamlineAuthorizer.CONF_CATALOG_SERVICE, securityCatalogService);
        authorizerConfig.put(DefaultStreamlineAuthorizer.CONF_ADMIN_PRINCIPALS, authorizerConf.getAdminPrincipals());
        authorizer.init(authorizerConfig);
        String filterClazzName = authorizerConf.getContainerRequestFilter();
        ContainerRequestFilter filter;
        if (StringUtils.isEmpty(filterClazzName)) {
            // default
            filter = new StreamlineKerberosRequestFilter();
        } else {
            filter = ((Class<ContainerRequestFilter>) Class.forName(filterClazzName)).newInstance();
        }
        LOG.info("Registering ContainerRequestFilter: {}", filter.getClass().getCanonicalName());
        environment.jersey().register(filter);
    } else {
        LOG.info("Authorizer config not set, setting noop authorizer");
        String noopAuthorizerClassName = "com.hortonworks.streamline.streams.security.impl.NoopAuthorizer";
        authorizer = ((Class<StreamlineAuthorizer>) Class.forName(noopAuthorizerClassName)).newInstance();
    }
    for (ModuleConfiguration moduleConfiguration : modules) {
        String moduleName = moduleConfiguration.getName();
        String moduleClassName = moduleConfiguration.getClassName();
        LOG.info("Registering module [{}] with class [{}]", moduleName, moduleClassName);
        ModuleRegistration moduleRegistration = (ModuleRegistration) Class.forName(moduleClassName).newInstance();
        if (moduleConfiguration.getConfig() == null) {
            moduleConfiguration.setConfig(new HashMap<String, Object>());
        }
        if (moduleName.equals(Constants.CONFIG_STREAMS_MODULE)) {
            moduleConfiguration.getConfig().put(Constants.CONFIG_CATALOG_ROOT_URL, catalogRootUrl);
        }
        Map<String, Object> initConfig = new HashMap<>(moduleConfiguration.getConfig());
        initConfig.put(Constants.CONFIG_AUTHORIZER, authorizer);
        initConfig.put(Constants.CONFIG_SECURITY_CATALOG_SERVICE, securityCatalogService);
        initConfig.put(Constants.CONFIG_SUBJECT, subject);
        if ((initConfig.get("proxyUrl") != null) && (configuration.getHttpProxyUrl() == null || configuration.getHttpProxyUrl().isEmpty())) {
            LOG.warn("Please move proxyUrl, proxyUsername and proxyPassword configuration properties under streams module to httpProxyUrl, " + "httpProxyUsername and httpProxyPassword respectively at top level in your streamline.yaml");
            configuration.setHttpProxyUrl((String) initConfig.get("proxyUrl"));
            configuration.setHttpProxyUsername((String) initConfig.get("proxyUsername"));
            configuration.setHttpProxyPassword((String) initConfig.get("proxyPassword"));
        }
        // pass http proxy information from top level config to each module. Up to them how they want to use it. Currently used in StreamsModule
        initConfig.put(Constants.CONFIG_HTTP_PROXY_URL, configuration.getHttpProxyUrl());
        initConfig.put(Constants.CONFIG_HTTP_PROXY_USERNAME, configuration.getHttpProxyUsername());
        initConfig.put(Constants.CONFIG_HTTP_PROXY_PASSWORD, configuration.getHttpProxyPassword());
        moduleRegistration.init(initConfig, fileStorage);
        if (moduleRegistration instanceof StorageManagerAware) {
            LOG.info("Module [{}] is StorageManagerAware and setting StorageManager.", moduleName);
            StorageManagerAware storageManagerAware = (StorageManagerAware) moduleRegistration;
            storageManagerAware.setStorageManager(storageManager);
        }
        if (moduleRegistration instanceof TransactionManagerAware) {
            LOG.info("Module [{}] is TransactionManagerAware and setting TransactionManager.", moduleName);
            TransactionManagerAware transactionManagerAware = (TransactionManagerAware) moduleRegistration;
            transactionManagerAware.setTransactionManager(transactionManager);
        }
        resourcesToRegister.addAll(moduleRegistration.getResources());
    }
    LOG.info("Registering resources to Jersey environment: [{}]", resourcesToRegister);
    for (Object resource : resourcesToRegister) {
        environment.jersey().register(resource);
    }
    environment.jersey().register(MultiPartFeature.class);
    final ErrorPageErrorHandler errorPageErrorHandler = new ErrorPageErrorHandler();
    errorPageErrorHandler.addErrorPage(Response.Status.UNAUTHORIZED.getStatusCode(), "/401.html");
    environment.getApplicationContext().setErrorHandler(errorPageErrorHandler);
}
Also used : ErrorPageErrorHandler(org.eclipse.jetty.servlet.ErrorPageErrorHandler) HashMap(java.util.HashMap) StorageManager(com.hortonworks.registries.storage.StorageManager) ArrayList(java.util.ArrayList) StreamlineAuthorizer(com.hortonworks.streamline.streams.security.StreamlineAuthorizer) DefaultStreamlineAuthorizer(com.hortonworks.streamline.streams.security.impl.DefaultStreamlineAuthorizer) Storable(com.hortonworks.registries.storage.Storable) AuthorizerConfiguration(com.hortonworks.streamline.webservice.configurations.AuthorizerConfiguration) ModuleConfiguration(com.hortonworks.streamline.webservice.configurations.ModuleConfiguration) StreamlineKerberosRequestFilter(com.hortonworks.streamline.streams.security.authentication.StreamlineKerberosRequestFilter) NOOPTransactionManager(com.hortonworks.registries.storage.NOOPTransactionManager) TransactionEventListener(com.hortonworks.registries.storage.transaction.TransactionEventListener) StreamlineConfigurationResource(com.hortonworks.streamline.webservice.resources.StreamlineConfigurationResource) SecurityCatalogService(com.hortonworks.streamline.streams.security.service.SecurityCatalogService) NOOPTransactionManager(com.hortonworks.registries.storage.NOOPTransactionManager) TransactionManager(com.hortonworks.registries.storage.TransactionManager) StorageManagerAware(com.hortonworks.registries.storage.StorageManagerAware) HttpConnectorFactory(io.dropwizard.jetty.HttpConnectorFactory) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) TransactionManagerAware(com.hortonworks.registries.storage.TransactionManagerAware) FileStorage(com.hortonworks.registries.common.util.FileStorage) ModuleRegistration(com.hortonworks.streamline.common.ModuleRegistration) DefaultServerFactory(io.dropwizard.server.DefaultServerFactory)

Example 3 with Storable

use of com.hortonworks.registries.storage.Storable in project registry by hortonworks.

the class InMemoryStorageManager method addOrUpdate.

@Override
public void addOrUpdate(Storable storable) {
    String namespace = storable.getNameSpace();
    PrimaryKey id = storable.getPrimaryKey();
    if (!storageMap.containsKey(namespace)) {
        storageMap.putIfAbsent(namespace, new ConcurrentHashMap<PrimaryKey, Storable>());
        nameSpaceClassMap.putIfAbsent(namespace, storable.getClass());
    }
    if (!storageMap.get(namespace).containsKey(id)) {
        nextId(namespace);
    }
    storageMap.get(namespace).put(id, storable);
}
Also used : PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) Storable(com.hortonworks.registries.storage.Storable)

Example 4 with Storable

use of com.hortonworks.registries.storage.Storable in project registry by hortonworks.

the class InMemoryStorageManager method find.

@Override
public <T extends Storable> Collection<T> find(final String namespace, final List<QueryParam> queryParams, final List<OrderByField> orderByFields) throws StorageException {
    List<T> storables = new ArrayList<>();
    if (queryParams == null) {
        Collection<T> collection = list(namespace);
        storables = Lists.newArrayList(collection);
    } else {
        Class<?> clazz = nameSpaceClassMap.get(namespace);
        if (clazz != null) {
            Map<PrimaryKey, Storable> storableMap = storageMap.get(namespace);
            if (storableMap != null) {
                for (Storable val : storableMap.values()) {
                    if (matches(val, queryParams, clazz)) {
                        storables.add((T) val);
                    }
                }
            }
        }
    }
    if (orderByFields != null && !orderByFields.isEmpty()) {
        storables.sort((storable1, storable2) -> {
            try {
                for (OrderByField orderByField : orderByFields) {
                    Comparable value1 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable1);
                    Comparable value2 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable2);
                    int compareTo;
                    // same values continue
                    if (value1 == value2) {
                        continue;
                    } else if (value1 == null) {
                        // value2 is non null
                        compareTo = -1;
                    } else if (value2 == null) {
                        // value1 is non null
                        compareTo = 1;
                    } else {
                        // both value and value2 non null
                        compareTo = value1.compareTo(value2);
                    }
                    if (compareTo == 0) {
                        continue;
                    }
                    return orderByField.isDescending() ? -compareTo : compareTo;
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            // all group by fields are matched means equal
            return 0;
        });
    }
    return storables;
}
Also used : ArrayList(java.util.ArrayList) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) Storable(com.hortonworks.registries.storage.Storable) AlreadyExistsException(com.hortonworks.registries.storage.exception.AlreadyExistsException) StorageException(com.hortonworks.registries.storage.exception.StorageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OrderByField(com.hortonworks.registries.storage.OrderByField)

Example 5 with Storable

use of com.hortonworks.registries.storage.Storable in project registry by hortonworks.

the class SchemaVersionLifecycleManager method deleteSchemaVersionBranchMapping.

private void deleteSchemaVersionBranchMapping(Long schemaVersionId) throws SchemaNotFoundException, SchemaLifecycleException {
    List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList();
    schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId.toString()));
    List<OrderByField> orderByFields = new ArrayList<>();
    orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
    Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields);
    if (storables == null || storables.isEmpty()) {
        LOG.debug("No need to delete schema version mapping as the database did a cascade delete");
        return;
    }
    if (storables.size() > 1) {
        List<String> branchNamesTiedToSchema = storables.stream().map(storable -> schemaBranchCache.get(SchemaBranchCache.Key.of(storable.getSchemaBranchId())).getName()).collect(Collectors.toList());
        throw new SchemaLifecycleException(String.format("Schema version with id : '%s' is tied with more than one branch : '%s' ", schemaVersionId.toString(), Arrays.toString(branchNamesTiedToSchema.toArray())));
    }
    storageManager.remove(new StorableKey(SchemaBranchVersionMapping.NAMESPACE, storables.iterator().next().getPrimaryKey()));
}
Also used : ObjectMapperUtils(com.hortonworks.registries.schemaregistry.utils.ObjectMapperUtils) SchemaBranchCache(com.hortonworks.registries.schemaregistry.cache.SchemaBranchCache) Arrays(java.util.Arrays) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) InitializedStateDetails(com.hortonworks.registries.schemaregistry.state.details.InitializedStateDetails) SchemaVersionLifecycleStates(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates) QueryParam(com.hortonworks.registries.common.QueryParam) InbuiltSchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.InbuiltSchemaVersionLifecycleState) LoggerFactory(org.slf4j.LoggerFactory) Storable(com.hortonworks.registries.storage.Storable) OrderByField(com.hortonworks.registries.storage.OrderByField) Hex(org.apache.commons.codec.binary.Hex) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) SchemaVersionLifecycleStateTransitionListener(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransitionListener) Map(java.util.Map) SchemaVersionLifecycleStateTransition(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) Logger(org.slf4j.Logger) SchemaVersionLifecycleStateAction(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateAction) SchemaVersionService(com.hortonworks.registries.schemaregistry.state.SchemaVersionService) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) Collection(java.util.Collection) SchemaVersionMergeException(com.hortonworks.registries.schemaregistry.errors.SchemaVersionMergeException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CustomSchemaStateExecutor(com.hortonworks.registries.schemaregistry.state.CustomSchemaStateExecutor) SchemaVersionLifecycleContext(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext) Collectors(java.util.stream.Collectors) StorageException(com.hortonworks.registries.storage.exception.StorageException) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) SchemaVersionInfoCache(com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache) SchemaVersionLifecycleStateMachine(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateMachine) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) Preconditions(com.google.common.base.Preconditions) SchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState) StorableKey(com.hortonworks.registries.storage.StorableKey) StorageManager(com.hortonworks.registries.storage.StorageManager) Collections(java.util.Collections) QueryParam(com.hortonworks.registries.common.QueryParam) OrderByField(com.hortonworks.registries.storage.OrderByField) StorableKey(com.hortonworks.registries.storage.StorableKey) ArrayList(java.util.ArrayList) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)

Aggregations

Storable (com.hortonworks.registries.storage.Storable)14 StorageManager (com.hortonworks.registries.storage.StorageManager)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 IntegrationTest (com.hortonworks.registries.common.test.IntegrationTest)3 AbstractStoreManagerTest (com.hortonworks.registries.storage.AbstractStoreManagerTest)3 StorableKey (com.hortonworks.registries.storage.StorableKey)3 StorableTest (com.hortonworks.registries.storage.StorableTest)3 HashSet (java.util.HashSet)3 CacheBuilder (com.google.common.cache.CacheBuilder)2 QueryParam (com.hortonworks.registries.common.QueryParam)2 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)2 CacheBackedStorageManager (com.hortonworks.registries.storage.CacheBackedStorageManager)2 OrderByField (com.hortonworks.registries.storage.OrderByField)2 PrimaryKey (com.hortonworks.registries.storage.PrimaryKey)2 StorageWriter (com.hortonworks.registries.storage.cache.writer.StorageWriter)2 StorageException (com.hortonworks.registries.storage.exception.StorageException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1