Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleHaUrlRewriteFunctionProcessor
linenumberstrue
public class HaUrlRewriteFunctionProcessor implements UrlRewriteFunctionProcessor<HaUrlRewriteFunctionDescriptor> {
    private HostMapperService hostMapperService;
    private HostMapper hostMapper = null;
    private String clusterName;

    @Override
    public String name() {
        return HaUrlRewriteFunctionDescriptor.FUNCTION_NAME;
    }
    @Override
    public void initialize(UrlRewriteEnvironment environment, HaUrlRewriteFunctionDescriptor descriptor) throws Exception {
        hostMapper = new HaBaseStrategyHostMapper();
        clusterName = environment.getAttribute(  GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE );
        GatewayServices services = environment.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE );
        if( clusterName != null && services != null ) {
            hostMapperService = services.getService( GatewayServices.HOST_MAPPING_SERVICE );
            if( hostMapperService != null ) {
                hostMapperService.registerHostMapperForCluster( clusterName, hostMapper );
            }
        }
    }
    @Override
    public void destroy() throws Exception {
        if( hostMapperService != null && clusterName != null ) {
            hostMapperService.removeHostMapperForCluster( clusterName );
        }
    }
    @Override
    public List<String> resolve(UrlRewriteContext context, List<String> parameters) throws Exception {
        List<String> result = null;
        if( parameters != null ) {
            result = new ArrayList<String>( parameters.size() );
            for( String parameter : parameters ) {
                switch( context.getDirection() ) {
                    case IN:
                        parameter = hostMapper.resolveInboundHostName( parameter );
                        break;
                    case OUT:
                        parameter = hostMapper.resolveOutboundHostName( parameter );
                        break;
                }
                result.add( parameter );
            }
        }
        return result;
    }
}

 

Managing multiple requests and thread safety

 

See Pic#4 for time diagramm for thread safe HA processing.

Image Added

Pic#4 - Time diagramm for thread safe HA processing

 

  1. Suppose we have two requests (Request#1 and Request#2). Request#1 starts for processing earlier than Request#2.
  2. Request#1 ends with timeout exception. This triggers failover. Class HaActiveServiceResolver has method resetStateToFailOver() which switches next URL in the list defined in <service> tag according to strategy.
  3. While switching in HaActiveServiceResolver is happening no one thread can perform a switch because resetStateToFailOver() is synchronized.
  4. Using getActiveService() method in HaActiveServiceResolver threads can get URL.

 

See Class diagramm for HaActiveServiceResolver.

Image Added

Pic#5 - Class diagramm