Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Configuration transporter #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

class eZFConfigurationTransporterSolr extends eZFConfigurationTransporter
{
public function push( $configuration )
{
/**
* synchronises elevate configuration across language shards in case of
* multiple language indexes, or the default one
*
* @TODO: handle exceptions properly
*/
$solr = new eZSolr();

if( $solr->UseMultiLanguageCores )
{
foreach ( $solr->SolrLanguageShards as $shard )
{
$this->synchronizeWithSolr( $shard, $configuration );
}
return true;
}
else
{
return $this->synchronizeWithSolr( new eZSolrBase(), $configuration );
}
}

/**
* Synchronizes the elevate configuration stored in the DB
* with the one actually used by Solr.
*/
protected function synchronizeWithSolr( $shard, $configuration )
{
if( $configuration )
{
try
{
$this->pushConfigurationToSolr( $shard, $configuration );
}
catch ( Exception $e )
{
eZFindElevateConfiguration::$lastSynchronizationError = $e->getMessage();
eZDebug::writeError( eZFindElevateConfiguration::$lastSynchronizationError, __METHOD__ );
return false;
}
}
else
{
$message = ezpI18n::tr( 'extension/ezfind/elevate', "Error while generating the configuration XML" );
eZFindElevateConfiguration::$lastSynchronizationError = $message;
eZDebug::writeError( $message, __METHOD__ );
return false;
}

return true;
}

/**
* Pushes the configuration XML to Solr through a custom requestHandler ( HTTP/ReST ).
* The requestHandler ( Solr extension ) will take care of reloading the configuration.
*/
protected function pushConfigurationToSolr( $shard, $configuration )
{
$params = array(
'qt' => 'ezfind',
eZFindElevateConfiguration::CONF_PARAM_NAME => $configuration
);


$result = $shard->pushElevateConfiguration( $params );

if ( ! $result )
{
$message = ezpI18n::tr( 'extension/ezfind/elevate', 'An unknown error occured in updating Solr\'s elevate configuration.' );
eZDebug::writeError( $message, __METHOD__ );
throw new Exception( $message );
}
elseif ( isset( $result['error'] ) )
{
eZDebug::writeError( $result['error'], __METHOD__ );
}
else
{
eZDebug::writeNotice( "Successful update of Solr's configuration.", __METHOD__ );
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

class eZFConfigurationTransporterZookeeperCli extends ezfConfigurationTransporter
{
protected $cli;
protected $configFilePath;

public function __construct( $parameters )
{
$this->cli = $parameters[ 'cli' ] ?? '/opt/zookeeper/bin/zkCli.sh';
$this->configFilePath = $parameters[ 'ConfigFilePath' ] ?? '/configs/ezp_collection_config/elevate.xml';

parent::__construct( $parameters );
}

/**
* @param string $elevateXmlString
* @return bool
*/
public function push( $elevateXmlString )
{
exec($this->cli . ' set '. $this->configFilePath . ' \'' . $elevateXmlString . '\'', $output, $exitCode );

if( $exitCode === 0 )
{
$solrBase = new eZSolrBase();
return $solrBase->reloadCollection();
}

return false;
}
}
33 changes: 33 additions & 0 deletions classes/ezfconfigurationtransporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class eZFConfigurationTransporter
{
protected $parameters;

public function __construct( $parameters )
{
$this->parameters = $parameters;
}

public function push( $configuration )
{
eZFindElevateConfiguration::$lastSynchronizationError = 'ConfigurationTransporter class does not exits.';
return false;
}

public static function factory()
{
$settings = eZINI::instance( 'solr.ini' );
$class = $settings->variable( 'SolrBase', 'ConfigurationTransporter' );
$parameters = $settings->variable( 'SolrBase', 'ConfigurationTransporterParameters' );

if( class_exists( $class ) )
{
return new $class( $parameters );
}
else
{
return new self( $parameters );
}
}
}
71 changes: 1 addition & 70 deletions classes/ezfindelevateconfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,37 +316,6 @@ public static function purge( $queryString = '' , $objectID = null, $languageCod
return parent::removeObject( self::definition(), $conds );
}

/**
* Synchronizes the elevate configuration stored in the DB
* with the one actually used by Solr.
*
* @return boolean true if the whole operation passed, false otherwise.
*/
public static function synchronizeWithSolr( $shard = null )
{
if ( self::generateConfiguration() )
{
try
{
self::pushConfigurationToSolr( $shard );
}
catch ( Exception $e )
{
self::$lastSynchronizationError = $e->getMessage();
eZDebug::writeError( self::$lastSynchronizationError, __METHOD__ );
return false;
}
}
else
{
$message = ezpI18n::tr( 'extension/ezfind/elevate', "Error while generating the configuration XML" );
self::$lastSynchronizationError = $message;
eZDebug::writeError( $message, __METHOD__ );
return false;
}
return true;
}

/**
* @return string
*/
Expand Down Expand Up @@ -439,44 +408,6 @@ protected static function getConfiguration()
return self::$configurationXML;
}

/**
* Pushes the configuration XML to Solr through a custom requestHandler ( HTTP/ReST ).
* The requestHandler ( Solr extension ) will take care of reloading the configuration.
*
* @see $configurationXML
* @return void
*/
protected static function pushConfigurationToSolr( $shard = null )
{
$params = array(
'qt' => 'ezfind',
self::CONF_PARAM_NAME => self::getConfiguration()
);

// Keep previous behaviour, but should not be needed
if ( $shard === null )
{
$shard = new eZSolrBase();
}

$result = $shard->pushElevateConfiguration( $params );

if ( ! $result )
{
$message = ezpI18n::tr( 'extension/ezfind/elevate', 'An unknown error occured in updating Solr\'s elevate configuration.' );
eZDebug::writeError( $message, __METHOD__ );
throw new Exception( $message );
}
elseif ( isset( $result['error'] ) )
{
eZDebug::writeError( $result['error'], __METHOD__ );
}
else
{
eZDebug::writeNotice( "Successful update of Solr's configuration.", __METHOD__ );
}
}

/**
* Generates a well-formed array of elevate-related query parameters.
*
Expand Down Expand Up @@ -515,4 +446,4 @@ public static function getRuntimeQueryParameters( $forceElevation = false, $enab
}
// Initialize the static property containing <eZINI> solr.ini
eZFindElevateConfiguration::$solrINI = eZINI::instance( 'solr.ini' );
?>

31 changes: 31 additions & 0 deletions classes/ezsolrbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,37 @@ function pushElevateConfiguration( $params )
return $this->rawSearch( $params );
}

/**
* @return string
*/
public function getCollectionName()
{
$parsedUrl = parse_url( $this->SearchServerURI );
if( isset( $parsedUrl[ 'path' ] ) )
{
$pathParts = explode( '/', $parsedUrl[ 'path' ] );
return $pathParts[ 2 ] ?? 'ezp-default';
}

return 'ezp-default';
}

/**
* @return bool
*/
public function reloadCollection()
{
$collectionName = $this->getCollectionName();

if( $collectionName )
{
$response = $this->rawSolrRequest( '/admin/collections?action=RELOAD&name=' . $collectionName );
return $response[ 'responseHeader' ][ 'status' ] === 0;
}

return false;
}

/**
* Proxy method to {@link self::sendHTTPRequest()}.
* Sometimes, an overloaded Solr server can result a timeout and drop the connection
Expand Down
7 changes: 4 additions & 3 deletions modules/ezfind/elevate.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@
// Synchronise Elevate configuration with Solr :
else if ( $http->hasPostVariable( 'ezfind-elevate-synchronise' ) )
{
$solr = new eZSolr();
//if ( eZFindElevateConfiguration::synchronizeWithSolr() )
if ( $solr->pushElevateConfiguration() )
$configurationTransporter = eZFConfigurationTransporter::factory();
$success = $configurationTransporter->push( eZFindElevateConfiguration::getElevateConfiguration() );

if ( $success )
{
$feedback['synchronisation_ok'] = true;
}
Expand Down
42 changes: 15 additions & 27 deletions search/plugins/ezsolr/ezsolr.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,10 +898,21 @@ public function removeObjectById( $contentObjectId, $commit = null, $commitWithi
$commit = true;
}

// 1: remove the assciated "elevate" configuration
eZFindElevateConfiguration::purge( '', $contentObjectId );
//eZFindElevateConfiguration::synchronizeWithSolr();
$this->pushElevateConfiguration();
// 1: remove the associated "elevate" configuration
if( eZFindElevateConfiguration::fetchConfigurationForObject(
$contentObjectId,
true,
null,
null,
true
)
)
{
eZFindElevateConfiguration::purge( '', $contentObjectId );

$configurationTransporter = eZFConfigurationTransporter::factory();
$configurationTransporter->push( eZFindElevateConfiguration::getElevateConfiguration() );
}

// @todo Remove if accepted. Optimize is bad on runtime.
$optimize = false;
Expand Down Expand Up @@ -1596,29 +1607,6 @@ private function initLanguageShards()

}


/**
* synchronises elevate configuration across language shards in case of
* multiple lnguage indexes, or the default one
*
* @TODO: handle exceptions properly
*/
public function pushElevateConfiguration()
{
if ( $this->UseMultiLanguageCores == true )
{
foreach ( $this->SolrLanguageShards as $shard )
{
eZFindElevateConfiguration::synchronizeWithSolr( $shard );
}
return true;
}
else
{
return eZFindElevateConfiguration::synchronizeWithSolr( $this->Solr );
}
}

/**
* Translates a solr response into result objects or a slightly modified array.
* The $asObjects parameter controls which of the 2 return formats get send back.
Expand Down
9 changes: 9 additions & 0 deletions settings/solr.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ Shards[]
#Shards[nor-NO]=http://localhost:8983/solr/nor-NO
#Shards[myforeignindex]=http://myotherhost:8983/solr

# valid options:
# eZFConfigurationTransporterSolr
# eZFConfigurationTransporterZookeeperCli
ConfigurationTransporter=eZFConfigurationTransporterSolr

# Parameters are send to ConfigurationTransporter constructor
#ConfigurationTransporterParameters[]
#ConfigurationTransporterParameters[cli]=/opt/zookeeper/bin/zkCli.sh
#ConfigurationTransporterParameters[ConfigFilePath]=/configs/ezp_collection_config/elevate.xml