概述
hadoop使用了protobuf作为默认的数据交换格式,但是hadoop也实现了插拔机制,允许用户使用其它的格式,如thrift等。所以,hadoop在制定接口和实现基于protobuf的底层模块时,使用了抽象工厂模式,并使用java反射对抽象工厂模式加以优化。
抽象工厂模式
抽象工厂模式用于生产不同产品族的全部产品。对于每一个产品族,都有一个具体工厂。而每一个具体工厂创建属于同一个产品族,但是分属于不同等级结构的产品。
使用反射优化的抽象工厂模式
在抽象工厂模式中,每个具体工厂角色都需要负责它所在的产品族中不同等级结构的产品对象的创建。有多少个产品等级结构,就会在工厂角色中发现多少个工厂方法。当产品等级结构的数量达到千位数,其工厂角色有千个以上的方法,代码就会变得很臃肿。我们可以使用java反射机制,同一产品族中不同等级结构的产品对象放在同一个包下,在工厂角色中只保留一个方法,该方法根据传入的抽象产品类名,加以约定的产品实现类后缀,得知其产品实现类的类名,再加以该工厂对应的产品族所在的包的包名, 得知该产品实现类的完整路径,然后反射创建相应的产品对象。
如上图所示,在包名后缀为“impl.pb”的包下的所有类,是属于protobuf产品族的下的所有产品对象。
RecordFactoryPBImpl源码
/**
* 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 org.apache.hadoop.yarn.factories.impl.pb;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
import org.apache.hadoop.yarn.factories.RecordFactory;
@Private
public class RecordFactoryPBImpl implements RecordFactory {
private static final String PB_IMPL_PACKAGE_SUFFIX = "impl.pb";
private static final String PB_IMPL_CLASS_SUFFIX = "PBImpl";
private static final RecordFactoryPBImpl self = new RecordFactoryPBImpl();
private Configuration localConf = new Configuration();
private ConcurrentMap<Class<?>, Constructor<?>> cache = new ConcurrentHashMap<Class<?>, Constructor<?>>();
private RecordFactoryPBImpl() {
}
public static RecordFactory get() {
return self;
}
@SuppressWarnings("unchecked")
@Override
public <T> T newRecordInstance(Class<T> clazz) {
Constructor<?> constructor = cache.get(clazz);
if (constructor == null) {
Class<?> pbClazz = null;
try {
pbClazz = localConf.getClassByName(getPBImplClassName(clazz));
} catch (ClassNotFoundException e) {
throw new YarnRuntimeException("Failed to load class: ["
+ getPBImplClassName(clazz) + "]", e);
}
try {
constructor = pbClazz.getConstructor();
constructor.setAccessible(true);
cache.putIfAbsent(clazz, constructor);
} catch (NoSuchMethodException e) {
throw new YarnRuntimeException("Could not find 0 argument constructor", e);
}
}
try {
Object retObject = constructor.newInstance();
return (T)retObject;
} catch (InvocationTargetException e) {
throw new YarnRuntimeException(e);
} catch (IllegalAccessException e) {
throw new YarnRuntimeException(e);
} catch (InstantiationException e) {
throw new YarnRuntimeException(e);
}
}
private String getPBImplClassName(Class<?> clazz) {
String srcPackagePart = getPackageName(clazz);
String srcClassName = getClassName(clazz);
String destPackagePart = srcPackagePart + "." + PB_IMPL_PACKAGE_SUFFIX;
String destClassPart = srcClassName + PB_IMPL_CLASS_SUFFIX;
return destPackagePart + "." + destClassPart;
}
private String getClassName(Class<?> clazz) {
String fqName = clazz.getName();
return (fqName.substring(fqName.lastIndexOf(".") + 1, fqName.length()));
}
private String getPackageName(Class<?> clazz) {
return clazz.getPackage().getName();
}
}