You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by mikezaccardo <gi...@git.apache.org> on 2015/08/04 18:57:49 UTC

[GitHub] incubator-brooklyn pull request: MySql master-slave cluster implem...

Github user mikezaccardo commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/783#discussion_r36212793
  
    --- Diff: software/database/src/main/java/brooklyn/entity/database/mysql/MySqlClusterImpl.java ---
    @@ -0,0 +1,445 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package brooklyn.entity.database.mysql;
    +
    +import java.util.Collection;
    +import java.util.Map;
    +import java.util.concurrent.Callable;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.atomic.AtomicInteger;
    +
    +import com.google.common.base.Function;
    +import com.google.common.base.Functions;
    +import com.google.common.base.Predicate;
    +import com.google.common.base.Predicates;
    +import com.google.common.base.Supplier;
    +import com.google.common.base.Suppliers;
    +import com.google.common.collect.ImmutableMap;
    +import com.google.common.collect.Iterables;
    +import com.google.common.reflect.TypeToken;
    +
    +import brooklyn.config.ConfigKey;
    +import brooklyn.enricher.Enrichers;
    +import brooklyn.entity.Entity;
    +import brooklyn.entity.basic.Attributes;
    +import brooklyn.entity.basic.EntityInternal;
    +import brooklyn.entity.basic.EntityLocal;
    +import brooklyn.entity.basic.EntityPredicates;
    +import brooklyn.entity.basic.ServiceStateLogic.ServiceNotUpLogic;
    +import brooklyn.entity.group.DynamicClusterImpl;
    +import brooklyn.entity.proxying.EntitySpec;
    +import brooklyn.event.AttributeSensor;
    +import brooklyn.event.SensorEvent;
    +import brooklyn.event.SensorEventListener;
    +import brooklyn.event.basic.DependentConfiguration;
    +import brooklyn.event.basic.Sensors;
    +import brooklyn.event.feed.function.FunctionFeed;
    +import brooklyn.event.feed.function.FunctionPollConfig;
    +import brooklyn.location.Location;
    +import brooklyn.util.collections.CollectionFunctionals;
    +import brooklyn.util.guava.Functionals;
    +import brooklyn.util.guava.IfFunctions;
    +import brooklyn.util.task.DynamicTasks;
    +import brooklyn.util.task.TaskBuilder;
    +import brooklyn.util.text.Identifiers;
    +import brooklyn.util.text.StringPredicates;
    +import brooklyn.util.time.Duration;
    +
    +// https://dev.mysql.com/doc/refman/5.7/en/replication-howto.html
    +
    +// TODO CREATION_SCRIPT_CONTENTS executed before replication setup so it is not replicated to slaves
    +// TODO Bootstrap slave from dump for the case where the binary log is purged
    +// TODO Promote slave to master
    +// TODO SSL connection between master and slave
    +// TODO DB credentials littered all over the place in file system
    +public class MySqlClusterImpl extends DynamicClusterImpl implements MySqlCluster {
    +    private static final AttributeSensor<Boolean> NODE_REPLICATION_INITIALIZED = Sensors.newBooleanSensor("mysql.replication_initialized");
    +
    +    private static final String MASTER_CONFIG_URL = "classpath:///brooklyn/entity/database/mysql/mysql_master.conf";
    +    private static final String SLAVE_CONFIG_URL = "classpath:///brooklyn/entity/database/mysql/mysql_slave.conf";
    +    private static final int MASTER_SERVER_ID = 1;
    +    private static final Predicate<Entity> IS_MASTER = EntityPredicates.configEqualTo(MySqlNode.MYSQL_SERVER_ID, MASTER_SERVER_ID);
    +
    +    @SuppressWarnings("serial")
    +    private static final AttributeSensor<Supplier<Integer>> SLAVE_NEXT_SERVER_ID = Sensors.newSensor(new TypeToken<Supplier<Integer>>() {},
    +            "mysql.slave.next_server_id", "Returns the ID of the next slave server");
    +    @SuppressWarnings("serial")
    +    private static final AttributeSensor<Map<String, String>> SLAVE_ID_ADDRESS_MAPPING = Sensors.newSensor(new TypeToken<Map<String, String>>() {},
    +            "mysql.slave.id_address_mapping", "Maps slave entity IDs to SUBNET_ADDRESS, so the address is known at member remove time.");
    +
    +    @Override
    +    public void init() {
    +        super.init();
    +        // Set id supplier in attribute so it is serialized
    +        setAttribute(SLAVE_NEXT_SERVER_ID, new NextServerIdSupplier());
    +        setAttribute(SLAVE_ID_ADDRESS_MAPPING, new ConcurrentHashMap<String, String>());
    +        if (getConfig(SLAVE_PASSWORD) == null) {
    +            setAttribute(SLAVE_PASSWORD, Identifiers.makeRandomId(8));
    +        } else {
    +            setAttribute(SLAVE_PASSWORD, getConfig(SLAVE_PASSWORD));
    +        }
    +        initSubscriptions();
    +    }
    +
    +    @Override
    +    public void rebind() {
    +        super.rebind();
    +        initSubscriptions();
    +    }
    +
    +    private void initSubscriptions() {
    +        subscribeToMembers(this, MySqlNode.SERVICE_PROCESS_IS_RUNNING, new NodeRunningListener(this));
    +        subscribe(this, MEMBER_REMOVED, new MemberRemovedListener());
    +    }
    +
    +    @Override
    +    protected void initEnrichers() {
    +        super.initEnrichers();
    +        propagateMasterAttribute(MySqlNode.HOSTNAME);
    +        propagateMasterAttribute(MySqlNode.ADDRESS);
    +        propagateMasterAttribute(MySqlNode.MYSQL_PORT);
    +        propagateMasterAttribute(MySqlNode.DATASTORE_URL);
    +
    +        addEnricher(Enrichers.builder()
    +                .aggregating(MySqlNode.DATASTORE_URL)
    +                .publishing(SLAVE_DATASTORE_URL_LIST)
    +                .computing(Functions.<Collection<String>>identity())
    +                .entityFilter(Predicates.not(IS_MASTER))
    +                .fromMembers()
    +                .build());
    +
    +        addEnricher(Enrichers.builder()
    +                .aggregating(MySqlNode.QUERIES_PER_SECOND_FROM_MYSQL)
    +                .publishing(QUERIES_PER_SECOND_FROM_MYSQL_PER_NODE)
    +                .fromMembers()
    +                .computingAverage()
    +                .defaultValueForUnreportedSensors(0d)
    +                .build());
    +    }
    +
    +    private void propagateMasterAttribute(AttributeSensor<?> att) {
    +        addEnricher(Enrichers.builder()
    +                .aggregating(att)
    +                .publishing(att)
    +                .computing(IfFunctions.ifPredicate(CollectionFunctionals.notEmpty())
    +                        .apply(CollectionFunctionals.firstElement())
    +                        .defaultValue(null))
    +                .entityFilter(IS_MASTER)
    +                .build());
    +    }
    +
    +    @Override
    +    protected EntitySpec<?> getFirstMemberSpec() {
    +        final EntitySpec<?> firstMemberSpec = super.getFirstMemberSpec();
    +        if (firstMemberSpec != null) {
    +            return applyDefaults(firstMemberSpec, Suppliers.ofInstance(MASTER_SERVER_ID), MASTER_CONFIG_URL, false);
    +        }
    +
    +        final EntitySpec<?> memberSpec = super.getMemberSpec();
    +        if (memberSpec != null) {
    +            if (!isKeyConfigured(memberSpec, MySqlNode.TEMPLATE_CONFIGURATION_URL.getConfigKey())) {
    +                return EntitySpec.create(memberSpec)
    +                        .configure(MySqlNode.MYSQL_SERVER_ID, MASTER_SERVER_ID)
    +                        .configure(MySqlNode.TEMPLATE_CONFIGURATION_URL, MASTER_CONFIG_URL);
    +            } else {
    +                return memberSpec;
    +            }
    +        }
    +
    +        return EntitySpec.create(MySqlNode.class)
    +                .displayName("MySql Master")
    +                .configure(MySqlNode.MYSQL_SERVER_ID, MASTER_SERVER_ID)
    +                .configure(MySqlNode.TEMPLATE_CONFIGURATION_URL, MASTER_CONFIG_URL);
    +    }
    +
    +    @Override
    +    protected EntitySpec<?> getMemberSpec() {
    +        Supplier<Integer> serverIdSupplier = getAttribute(SLAVE_NEXT_SERVER_ID);
    +
    +        EntitySpec<?> spec = super.getMemberSpec();
    +        if (spec != null) {
    +            return applyDefaults(spec, serverIdSupplier, SLAVE_CONFIG_URL, true);
    +        }
    +
    +        return EntitySpec.create(MySqlNode.class)
    +                .displayName("MySql Slave")
    +                .configure(MySqlNode.MYSQL_SERVER_ID, serverIdSupplier.get())
    +                .configure(MySqlNode.TEMPLATE_CONFIGURATION_URL, SLAVE_CONFIG_URL)
    +                // block inheritance, only master should execute the creation script
    +                .configure(MySqlNode.CREATION_SCRIPT_URL, (String) null)
    +                .configure(MySqlNode.CREATION_SCRIPT_CONTENTS, (String) null);
    +    }
    +
    +    private EntitySpec<?> applyDefaults(EntitySpec<?> spec, Supplier<Integer> serverId, String configUrl, boolean resetCreationScript) {
    --- End diff --
    
    Might be nice to have `applyMasterDefaults` and `applySlaveDefaults` methods which call a third helper method with most of this content.
    
    I like the clarity of that over just the `resetCreationScript` variable which is 1:1 with master / slave.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---