From a38ee5f0a77eb6f678f3f0f11e72183398eed81b Mon Sep 17 00:00:00 2001 From: yossipapi Date: Sun, 20 Oct 2024 17:18:16 +0300 Subject: [PATCH 1/2] No-Plat: Source map will not return by default + serve latest bundle if failed to create new one --- .../actions/embedPlaykitJsAction.class.php | 116 +++++++++++++++--- alpha/lib/model/uiConf.php | 12 +- 2 files changed, 112 insertions(+), 16 deletions(-) diff --git a/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php b/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php index 82f8657cf3e..6db356dc561 100644 --- a/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php +++ b/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php @@ -16,6 +16,7 @@ class embedPlaykitJsAction extends sfAction const REGENERATE_PARAM_NAME = "regenerate"; const IFRAME_EMBED_PARAM_NAME = "iframeembed"; const AUTO_EMBED_PARAM_NAME = "autoembed"; + const INCLUDE_SOURCE_MAP_PARAM_NAME = 'includeSourceMap'; const LATEST = "{latest}"; const BETA = "{beta}"; const CANARY = "{canary}"; @@ -47,6 +48,7 @@ class embedPlaykitJsAction extends sfAction private $playerConfig = null; private $uiConfUpdatedAt = null; private $regenerate = false; + private $includeSourceMap = "false"; private $uiConfTags = array(self::PLAYER_V3_VERSIONS_TAG); public function execute() @@ -56,16 +58,41 @@ public function execute() $this->initMembers(); + $updateUiConfBundleCacheKey = true; $bundleContent = $this->bundleCache->get($this->bundle_name); $i18nContent = $this->bundleCache->get($this->bundle_i18n_name); $extraModulesNames = unserialize($this->bundleCache->get($this->bundle_extra_modules_names)); if (!$bundleContent || $this->regenerate) { - list($bundleContent, $i18nContent, $extraModulesNames) = kLock::runLocked($this->bundle_name, array("embedPlaykitJsAction", "buildBundleLocked"), array($this), 2, 30); + try + { + list($bundleContent, $i18nContent, $extraModulesNames, $updateUiConfBundleCacheKey) = kLock::runLocked($this->bundle_name, array("embedPlaykitJsAction", "buildBundleLocked"), array($this), 2, 30); + } + catch (kCoreException $ex) + { + switch ($ex->getCode()) + { + case kCoreException::LOCK_TIMED_OUT: + list($bundleContent, $i18nContent, $extraModulesNames, $updateUiConfBundleCacheKey) = $this->tryServingExistingCacheVersion($this, KExternalErrors::BUNDLE_CREATION_FAILED, "Failed to build bundle in allocated time"); + if(!$bundleContent) + { + KExternalErrors::dieError(KExternalErrors::BUNDLE_CREATION_FAILED, "Failed to serve bundle content"); + } + break; + default: + KExternalErrors::dieError(KExternalErrors::BUNDLE_CREATION_FAILED, "Failed to serve bundle content with unknown error"); + } + } } $lastModified = $this->getLastModified($bundleContent); + + //Try saving bundle content cache key if needed for serving old version in case of an error while generating new one + if($updateUiConfBundleCacheKey) + { + $this->saveCurrentBundleCacheKey($this); + } //Format bundle content $bundleContent = $this->formatBundleContent($bundleContent, $i18nContent, $extraModulesNames); @@ -88,7 +115,7 @@ public static function buildBundleLocked($context) { $i18nContent = $context->bundleCache->get($context->bundle_i18n_name); $extraModulesNames = unserialize($context->bundleCache->get($context->bundle_extra_modules_names)); - return array($bundleContent, $i18nContent, $extraModulesNames); + return array($bundleContent, $i18nContent, $extraModulesNames, false); } } @@ -99,25 +126,31 @@ public static function buildBundleLocked($context) KExternalErrors::dieError(KExternalErrors::BUNDLE_CREATION_FAILED, $config . " wrong config object"); } - $url = $context->bundlerUrl . "/build?config=" . base64_encode($config) . "&name=" . $context->bundle_name . "&source=" . base64_encode($context->sourcesPath); + $url = $context->bundlerUrl . "/build?config=" . base64_encode($config) . "&name=" . $context->bundle_name . "&source=" . base64_encode($context->sourcesPath) . "&includeSourceMap=" . $context->includeSourceMap; $content = KCurlWrapper::getContent($url, array('Content-Type: application/json'), true); - if (!$content) { - KExternalErrors::dieError(KExternalErrors::BUNDLE_CREATION_FAILED, $config . " failed to get content from bundle builder"); + return $context->tryServingExistingCacheVersion($context, KExternalErrors::BUNDLE_CREATION_FAILED, $config . " failed to get content from bundle builder"); } $content = json_decode($content, true); - if (isset($content['status'])) { - if ($content['status'] != 0) { + if (isset($content['status'])) + { + if ($content['status'] != 0) + { $message = $content['message']; - KExternalErrors::dieError(KExternalErrors::BUNDLE_CREATION_FAILED, $config . ". " . $message); - } else { + return $context->tryServingExistingCacheVersion($context, KExternalErrors::BUNDLE_CREATION_FAILED, $config . ". " . $message); + } + else + { $content = $content['payload']; } - } else { - if (!$content || !$content['bundle']) { - KExternalErrors::dieError(KExternalErrors::BUNDLE_CREATION_FAILED, $config . " bundle created with wrong content"); + } + else + { + if (!$content || !$content['bundle']) + { + return $context->tryServingExistingCacheVersion($context, KExternalErrors::BUNDLE_CREATION_FAILED, $config . " bundle created with wrong content"); } } @@ -137,8 +170,8 @@ public static function buildBundleLocked($context) { KalturaLog::log("Error - failed to save bundle content in cache for config [".$config."]"); } - - return array($bundleContent, $i18nContent, $extraModulesNames); + + return array($bundleContent, $i18nContent, $extraModulesNames, true); } private static function getExtraModuleNames($extraModules = array()) @@ -777,8 +810,11 @@ private function initMembers() if (!$this->partner) KExternalErrors::dieError(KExternalErrors::PARTNER_NOT_FOUND); - //Get should force regenration + //Get should force regeneration $this->regenerate = $this->getRequestParameter(self::REGENERATE_PARAM_NAME); + + //Should we include player source map in the request result + $this->includeSourceMap = $this->getRequestParameter(self::INCLUDE_SOURCE_MAP_PARAM_NAME, "false"); //Get the list of partner 0 uiconf tags for uiconfs that contain {latest} and {beta} lists $embedPlaykitConf = kConf::getMap(kConfMapNames::EMBED_PLAYKIT); @@ -865,4 +901,54 @@ public function getRequestParameter($name, $default = null) $returnValue = parent::getRequestParameter($name, $default); return $returnValue ? $returnValue : $default; } + + protected function saveCurrentBundleCacheKey($context) + { + //Avoid updating current cache hash on the ui conf if dynamic params affecting the result are being sent + $versions = $this->getRequestParameter(self::VERSIONS_PARAM_NAME); + if($versions) + { + return; + } + + if($context->bundle_name != $context->uiConf->getCurrentCacheKey()) + { + //Avoid system load by controlling the percentage of ui confs how will get this update + if(rand(0, 100) < kConf::get("current_version_save_ration", kConfMapNames::EMBED_PLAYKIT, 30)) + { + $context->uiConf->setCurrentCacheKey($context->bundle_name); + $context->uiConf->save(); + } + else + { + KalturaLog::debug("Skipping ui conf update to avoid unnecessary system load"); + } + } + } + + protected function tryServingExistingCacheVersion($context, $errCode, $message = null) + { + $existingCacheVersion = $context->uiConf->getCurrentCacheKey(); + KalturaLog::debug("TTT: existingCacheVersion [$existingCacheVersion]"); + if(!$existingCacheVersion) + { + KExternalErrors::dieError($errCode, $message); + } + + $i18nContent = null; + $extraModulesNames = null; + $bundleContent = $context->bundleCache->get($existingCacheVersion); + if ($bundleContent) + { + $i18nContent = $context->bundleCache->get($existingCacheVersion . "_i18n"); + $extraModulesNames = unserialize($context->bundleCache->get($existingCacheVersion . "_extramodules")); + } + + if(!$bundleContent) + { + KExternalErrors::dieError($errCode, $message); + } + + return array($bundleContent, $i18nContent, $extraModulesNames, false); + } } diff --git a/alpha/lib/model/uiConf.php b/alpha/lib/model/uiConf.php index 7939257a0b2..31c7d327853 100644 --- a/alpha/lib/model/uiConf.php +++ b/alpha/lib/model/uiConf.php @@ -116,8 +116,8 @@ class uiConf extends BaseuiConf implements ISyncableFile, IRelatedObject const CUSTOM_DATA_CON_FILE_VERSION = 'conf_file_version'; const CUSTOM_DATA_CONF_FILE_FEATURES_VERSION = 'conf_file_features_version'; - const CUSTOM_DATA_V2REDIRECT = "v2redirect"; + const CUSTOM_DATA_CURRENT_CACHE_KEY = 'current_cache_key'; public function save(PropelPDO $con = null) { @@ -827,4 +827,14 @@ public function setV2Redirect($v) KalturaLog::log(JSON_encode($v)); return $this->putInCustomData(self::CUSTOM_DATA_V2REDIRECT, $v); } + + public function getCurrentCacheKey() + { + return $this->getFromCustomData( self::CUSTOM_DATA_CURRENT_CACHE_KEY); + } + + public function setCurrentCacheKey($v) + { + return $this->putInCustomData(self::CUSTOM_DATA_CURRENT_CACHE_KEY, $v); + } } From bb50013ce8235632335dcbb60398ac5500680832 Mon Sep 17 00:00:00 2001 From: yossipapi Date: Tue, 22 Oct 2024 11:51:26 +0300 Subject: [PATCH 2/2] Remove temp debug line --- .../modules/extwidget/actions/embedPlaykitJsAction.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php b/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php index 6db356dc561..7a29928c8d8 100644 --- a/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php +++ b/alpha/apps/kaltura/modules/extwidget/actions/embedPlaykitJsAction.class.php @@ -929,7 +929,6 @@ protected function saveCurrentBundleCacheKey($context) protected function tryServingExistingCacheVersion($context, $errCode, $message = null) { $existingCacheVersion = $context->uiConf->getCurrentCacheKey(); - KalturaLog::debug("TTT: existingCacheVersion [$existingCacheVersion]"); if(!$existingCacheVersion) { KExternalErrors::dieError($errCode, $message);